## CCO '01 P1 - The Monkey Dance

View as PDF

Points: 10
Time limit: 2.0s
Memory limit: 64M

Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig
##### Canadian Computing Competition: 2001 Stage 2, Day 1, Problem 1

The director of Hind Circus has decided to add a new performance, the monkey dance, to his show. The monkey dance is performed simultaneously by monkeys. There are circles drawn on the ground, labelled from to . In the beginning, each monkey sits on a different circle. There are arrows drawn from circle to circle in such a way that there is exactly one arrow pointing toward each ring and exactly one arrow pointing away from each ring.

When the show begins, at each whistle of the ringmaster, all the monkeys simultaneously jump from their respective circles to other circles following the arrows from their respective current circles. This is one step of the dance. The dance ends when all the monkeys have returned to the circles where they initially started. How long does the dance last?

#### Input Specification

The input may have multiple cases. Each case consists of the value of on a line, followed by lines, each with a pair of integers between and . The pair , means that there is an arrow from circle to circle . The input ends with 0 for the value of .

#### Output Specification

For each case, simply output the number of steps in the dance. Each output should be on a separate line.

#### Sample Input

5
1 2
2 3
3 1
4 5
5 4
4
1 4
2 3
4 1
3 2
0

#### Sample Output

6
2