#C1107. Find the Shortest Loop on a Spaceship Path
Find the Shortest Loop on a Spaceship Path
Find the Shortest Loop on a Spaceship Path
You are given a sequence of coordinates representing the path of a spaceship. Your task is to determine whether the path contains any loop, and if so, output the length of the shortest loop. A loop is defined when the spaceship revisits a coordinate that it has been to before. Formally, if the ship visits coordinate \( (x, y) \) at index \( i \) and later revisits the same coordinate at index \( j \) (with \( j > i \)), then the loop length is defined as \( j - i + 1 \). If no such loop exists, output \(-1\).
Note: The input is given in standard input (stdin) and the output should be printed to standard output (stdout).
inputFormat
The first line contains an integer \( n \) representing the number of coordinates. Each of the next \( n \) lines contains two space-separated integers representing the coordinates \( x \) and \( y \) of the spaceship at that step.
outputFormat
Output a single integer which is the length of the shortest loop based on the definition above. If no loop exists, output \(-1\).
## sample5
0 0
1 1
0 1
1 0
0 0
5