Permutation logarithm
Hard
Execution time limit is 1 second
Runtime memory usage limit is 256 megabytes
Given two permutations (p = (p_1, p_2, ..., p_N)) and (q = (q_1, q_2, ..., q_N)) of the numbers from (1) to (N), your task is to determine the smallest non-negative integer (d) such that applying the permutation (p) (d) times to the sequence ((1, 2, ..., N)) results in the sequence (q).
Input
The input begins with a single line containing the natural number (N) ((1 N 2 10^5)). The next two lines provide the permutations (p) and (q). Each line contains (N) integers, representing the respective permutation.
Output
Output the smallest non-negative integer (d) for which (p^d = q). If no such (d) exists, print (-1).
Examples
Input #1
Answer #1
Submissions 20
Acceptance rate 5%