Hide

Problem D
Hogwarts

The Hogwarts School of Witchcraft and Wizardry is the home of many students during the school year. The school has many rooms connected by corridors and stairs. Each room has four exits labelled by the integers $1, 2, 3$ or $4$. Some exits lead to another room, some of them are blocked, and some even lead back to the same room you just came from.

New students often have difficulty finding their way, especially since the corridors and stairs are regularly moving, disconnecting and reconnecting different rooms! Luckily, these reconfigurations only take place when no one is walking in the school. All you want to know is how to get from the entrance to the dormitory. A senior student has given you instructions as a sequence of numbers among $1, 2, 3, 4$. The first number in the sequence is the exit to take from the starting room. The second number is the exit to take from the second room in the path, and so on. If at any point the indicated exit is blocked, you go back to the entrance and give up. To be successful you must arrive at the dormitory at the end of the entire sequence. Even if it appears you have reached the dormitory before the entire sequence is followed, you are not sure if that is an illusion. Therefore you follow the entire sequence.

You carefully followed the instructions and arrived at the dormitory. However, the way the rooms are connected to each other has changed after the senior student gave you the instructions, and you just happen to arrive at the same destination even if the rooms you encountered along the way may be completely different.

You wonder if you are just lucky, or if the reconfiguration of the corridors and stairs ensures that the instructions still lead you to the same destination. Isn’t that magical?

You will be given a configuration of the school when the senior student walked from the entrance to the dormitory, as well as the configuration of the school when you start following the given instructions. You want to know if every possible sequence of instructions that led the senior student to the dormitory will also lead you to the dormitory in the configuration you walk through. Both the senior student and you start walking from the entrance of the school.

Input

The first line of input contains a single integer $n$ ($2 \leq n \leq 1\, 000$), indicating the number of rooms in the school. The rooms are numbered $1$ to $n$, where room $1$ is the entrance and room $n$ is the dormitory.

The next $n$ lines of input describe the configuration of the school when the senior student walked to the dormitory, followed by another $n$ lines describing the configuration of the school when you start to walk to the dormitory.

The $i^\textrm {th}$ line in the school configuration consists of four non-negative integers, indicating which room exits $1, 2, 3$ and $4$ lead to. If the room number is $0$, the corresponding exit is blocked.

Output

If it is not possible for the senior student to walk from the entrance to the dormitory, display Impossible.

If it is possible, display Yes if you can get from the entrance to the dormitory by following any sequence of instructions taking the senior student from the entrance to the dormitory. Otherwise, display No.

Sample Input 1 Sample Output 1
4
1 1 1 2
2 2 2 3
3 3 3 4
0 0 0 0
2 2 2 2
3 3 3 3
4 4 4 4
4 4 4 4
Yes
Sample Input 2 Sample Output 2
4
1 1 1 2
2 2 2 3
3 3 3 4
0 0 0 0
2 2 2 2
3 3 3 3
4 4 4 4
0 0 0 0
No

Please log in to submit a solution to this problem

Log in