#Lutece0333. SequenceSync

SequenceSync

Migrated from Lutece 333 SequenceSync

All parts of this problem, including description, images, samples, data and checker, might be broken. If you find bugs in this problem, please contact the admins.

Description

State machines occur frequently in computer science literature. At any given point in time, a state machine is in a particular state pp. After receiving an input symbol, the machine updates its state. The potential input symbols in this problem are 00, 11, 22, or 33. Give you a transition list describes how the state machine MM behaves. Each element of the transition list will contain 44 space delimited integers. The integers in element ii (00-based) describe how MM reacts to input while in state ii. For example, if element 33 is 0 4 2 3, then MM will transition from state 33 to state 00, 44, 22, or 33 depending on whether 00, 11, 22, or 33 are received as input, respectively. Our state machine MM is unfortunately out of control. We do not know which state MM is currently in. To rectify the situation, we want to find a string ss and a state hh such that, after feeding ss into MM one symbol at a time, MM will definitely be in state hh. Considering all possible pairs of ss and hh, output the length of the shortest ss. If no such pairs exist, output 1-1.

Input

There are multi-cases. On the first line of each case, there is a integer nn (n20n\leq 20), the size of the transition list. Then nn lines followed, each line contains four integers ww, xx, yy and zz where ww, xx, yy, and zz are integers, between 00 and n1n-1 inclusive.

Output

For each case, output the length of the shortest ss. If no such pairs exist, output 1-1.

Samples

3
1 1 1 1
1 1 1 2
0 2 2 2
4
1 1 1 1
2 2 2 2
3 3 3 3
0 0 0 0
2
-1

Resources

2011寒假训练(一)(Not Original)