Statement
Researcher Isadora loves playing cards with her friends. More specifically, she plays a version called Baralho Alho, in which there are cards (duplicates are allowed). Initially, the cards are in a specific order: the -th card has value . Two cards are considered equal if they have the same value.
Before the game starts, Isadora declares: “I always shuffle Baralho Alho.” Naively, her friends agree and let her command the shuffling. Little do they know that Researcher Isadora loves to cheat. Her goal is to shuffle in such a way that, at the end, the -th card has value .
However, she knows only one kind of shuffle: it maps the card originally at position to position . For example, if , the first card goes to the third position, the second remains in place, the third goes to the fourth, and the fourth goes to the first. Thus, if the initial deck is , applying the shuffle produces .
Even with this limitation, Isadora is quite intelligent and plans to repeat the shuffle several times to reach new deck configurations.
Given , , and , determine the minimum number of times Isadora must apply the shuffle so that the deck reaches the desired order. If this is impossible, print IMPOSSIVEL. If the minimum number of shuffles is greater than , print DEMAIS.
Input
The first line contains (). The second line contains (), the initial deck. The third contains (), the target deck. The fourth contains distinct integers (), indicating that the card at position moves to position .
Output
Print the minimum number of shuffles needed. If it is impossible, print IMPOSSIVEL. If the minimum is greater than , print DEMAIS.
Examples
Input
6
8 6 5 5 1 3
5 1 8 5 3 6
2 3 6 5 1 4
Output
2
The configurations are:
- :
8 6 5 5 1 3; - :
1 8 6 3 5 5; - :
5 1 8 5 3 6.
Input
2
3 3
3 3
1 2
Output
0
The deck is already in the desired configuration.
Input
5
6 3 8 4 2
3 6 4 2 8
2 1 4 5 3
Output
5
Input
4
1 2 1 2
1 2 2 1
2 1 4 3
Output
IMPOSSIVEL
Input
3
1 2 3
2 1 4
1 2 3
Output
IMPOSSIVEL
Tutorial
Required topics: permutations, string matching, and the Chinese remainder theorem.
Turning cycles into congruences
Decompose the permutation into cycles and handle each cycle independently. Applying the shuffle once rotates the values along every cycle by one position. Therefore, for each cycle, we need to find which rotations of its initial value sequence equal its target sequence.
Duplicate the initial sequence and search for the target sequence inside it with KMP, the Z-function, or another linear string-matching algorithm. If no rotation matches for any cycle, the answer is IMPOSSIVEL.
If a cycle admits shift and its value sequence has minimal period , all its valid shifts are exactly
We now have a system of congruences, one from each cycle, and need to combine them using the generalized Chinese remainder theorem. The complication is that their least common multiple can become enormous and overflow ordinary integer types. We should first decide whether the complete system is consistent, and only then merge congruences until the modulus becomes large enough.
Checking whether a solution exists
There are two useful approaches.
Approach 1: pairwise compatibility
After merging congruences with the same modulus, there are only distinct moduli. This follows because the moduli do not exceed their corresponding cycle lengths, and the sum of all distinct cycle lengths is at most .
Two congruences
are compatible if and only if
A system of congruences is solvable if and only if every pair is compatible. Thus, after deduplication, checking every pair takes time in total. See the pairwise solvability criterion for a proof.
Approach 2: split into prime powers
Factor each modulus into distinct prime-power factors:
Because these factors are pairwise coprime, the congruence
is equivalent to the system
Do this for every original congruence, then group the resulting conditions by prime. For one fixed prime , we obtain conditions of the form
First, equal powers must have equal residues. Conditions at different powers must also agree after reduction to the smaller power: for example, implies , so it is incompatible with .
If every prime group is internally consistent, keep only its condition with the largest exponent. The remaining moduli are pairwise coprime, so the Chinese remainder theorem guarantees that the complete system has a solution.
Avoiding overflow and deciding DEMAIS
Once consistency is known, merge congruences one at a time with generalized CRT. Maintain the smallest nonnegative solution and the combined modulus . All solutions processed so far are
Stop as soon as . Test against all original congruences:
- if it satisfies all of them and , print ;
- otherwise, every other solution is at least , so print
DEMAIS.
The earlier consistency check is essential: without it, stopping early could incorrectly print DEMAIS for a system that is actually impossible.
Cycle decomposition and string matching take . With the pairwise compatibility approach, feasibility also takes after deduplication; factoring for the prime-power approach can likewise be supported by a smallest-prime-factor sieve. The final capped CRT merge is linear in the number of distinct congruences.