#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 . After receiving an input symbol, the machine updates its state. The potential input symbols in this problem are , , , or . Give you a transition list describes how the state machine behaves. Each element of the transition list will contain space delimited integers. The integers in element (-based) describe how reacts to input while in state . For example, if element is 0 4 2 3
, then will transition from state to state , , , or depending on whether , , , or are received as input, respectively. Our state machine is unfortunately out of control. We do not know which state is currently in. To rectify the situation, we want to find a string and a state such that, after feeding into one symbol at a time, will definitely be in state . Considering all possible pairs of and , output the length of the shortest . If no such pairs exist, output .
Input
There are multi-cases. On the first line of each case, there is a integer (), the size of the transition list. Then lines followed, each line contains four integers , , and where , , , and are integers, between and inclusive.
Output
For each case, output the length of the shortest . If no such pairs exist, output .
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)