QOJ.ac

QOJ

时间限制: 1 s 内存限制: 128 MB 总分: 100

#10657. Shuffling

统计

Bajtazar gave his son, Bajtek, a deck of cards as a gift. The deck consists of $n$ cards, numbered from 1 to $n$.

Bajtek was very happy with the gift. He spent the whole evening in his room playing with the cards, shuffling them. He became so skilled that every time he shuffled, the result was the same: the card at position $k$ (for $1 \le k \le n$) always moved to position $a_k$ (where $1 \le a_k \le n$).

At some point, Bajtazar came into Bajtek's room and said it was time to sleep. Bajtek begged his father to show him how to shuffle cards before going to bed. Bajtazar then shuffled the cards such that the card at position $k$ moved to position $b_k$ (where $1 \le k, b_k \le n$).

Bajtek watched with admiration at how skillfully his father shuffled the cards. He wanted to be able to do the same! Unfortunately, Bajtek is still small and cannot shuffle like his father. However, he came up with the idea of trying to repeat his own shuffling method several times to eventually achieve the same arrangement as his father's shuffle.

Now the boy cannot sleep because he is wondering if this is even possible. Help him!

Input

The first line of input contains a single integer $n$ ($2 \le n \le 1\,000\,000$). The second and third lines contain the descriptions of the permutations $a_1, \ldots, a_n$ and $b_1, \ldots, b_n$, which are sequences of $n$ distinct integers from 1 to $n$. You may assume that these two permutations are different.

Output

The first and only line of output should contain one word, TAK or NIE, depending on whether there exists an integer $k > 1$ such that repeating Bajtek's shuffle $k$ times results in the same arrangement as Bajtazar's shuffle.

Examples

Input 1

4
2 4 3 1
1 2 3 4

Output 1

TAK

Input 2

4
1 2 3 4
2 4 3 1

Output 2

NIE

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.