A defective card shuffling machine always shuffles a deck in the exact same way. Label the cards $1, 2, 3, \dots, N$, where $N$ is the total number of cards (copies of the same physical card coming from different decks count as different cards). Because the machine is broken, feeding the same set of cards through it always applies one fixed rearrangement.
You can still produce many different orderings by running the deck through the machine zero or more times, feeding each output straight back in without reordering it. For example, if the machine turns $1, 2, 3, 4$ into $2, 3, 4, 1$, then running that result through again gives $3, 4, 1, 2$.
Not every ordering can be reached this way. Given the machine's fixed shuffle and a desired target ordering, determine the fewest passes through the machine needed to reach the target ordering, or report that it is impossible.
The input contains several test cases. Each case consists of three lines:
The input ends with a line containing $N = 0$, which must not be processed.
For each case, print on its own line the minimum number of passes (zero or more) through the machine required to reach the target ordering, or $-1$ if it is impossible. You may assume the answer always fits in a signed 32-bit integer.