This page is still under construction.

Parts of this page are still being built. What you see may change.

Shuffling Cards

Time limit1sMemory limit128 MB

Summary
Decide whether permutation b equals some power a^k of permutation a with k greater than 1.
Level

Medium6 of 10

Topics
Math, Combinatorics, Implementation
Solved
No attempts yet

Problem

Bajtazar gave his son Bajtek a deck of cards as a present. The deck has nn cards, numbered from 11 to nn.

Bajtek loved the gift. He spent the whole evening in his room shuffling the deck, and he grew so practiced that every shuffle came out exactly the same way: during a shuffle the card at position kk (for 1≤k≤n1 \le k \le n) always moved to position aka_k (with 1≤ak≤n1 \le a_k \le n).

At some point Bajtek's father came in and said it was time for bed. Bajtek begged him to show, one last time before sleep, how cards should really be shuffled. So Bajtazar shuffled them so that the card at position kk ended up at position bkb_k (again 1≤k,bk≤n1 \le k, b_k \le n).

Bajtek admired how skillfully his father shuffled and wished he could do the same. He is still little, though, and cannot shuffle the way his father does. But he had an idea: he will repeat his own shuffle several times, hoping that in the end the deck ends up in the same arrangement as after his father's shuffle.

Now the boy cannot fall asleep, wondering whether this is even possible. Help him!

In other words, decide whether the permutation bb can be obtained by applying the permutation aa some number of times greater than one.

Input

The first line contains a single integer nn (2≤n≤1062 \le n \le 10^6). The second and third lines describe the permutations a1,…,ana_1, \dots, a_n and b1,…,bnb_1, \dots, b_n: each is a sequence of nn pairwise distinct integers from 11 to nn. You may assume that the two permutations are different.

Output

Print a single word: TAK if there exists an integer k>1k > 1 such that repeating Bajtek's shuffle kk times produces exactly Bajtazar's shuffle, or NIE otherwise. (TAK and NIE are the Polish words for "yes" and "no".)

Examples4

  1. Example 1

    Input
    4
    2 4 3 1
    1 2 3 4
    
    Expected output
    TAK
    
  2. Example 2

    Input
    4
    1 2 3 4
    2 4 3 1
    
    Expected output
    NIE
    
  3. Example 3

    Input
    2
    2 1
    1 2
    
    Expected output
    TAK
    
  4. Example 4

    Input
    2
    1 2
    2 1
    
    Expected output
    NIE