The Euclidean Algorithm

No attempts yetTime limit1sMemory limit128 MB

Problem

The famous Euclidean algorithm is found in Book VII of the Elements, written around 300 B.C. by the Greek mathematician Euclid. The story goes that King Ptolemy, having looked through the Elements, hopefully asked Euclid whether there was a shorter way to geometry, and Euclid severely answered: "In geometry there is no royal road!" We should probably not blame the King for looking for a short cut, because the Elements runs to thirteen books. The books consist mainly of the mathematical knowledge Euclid amassed, plus some discoveries of his own. Euclid's great achievement is the beautifully systematic presentation of the material as an organic whole. The Elements remained a standard work for over two thousand years. (see Episodes from the Early History of Mathematics, Asger Aaboe)

The modern Euclidean algorithm is usually presented like this.

  1. Let AA and BB be integers with A>B0A > B \geq 0.
  2. If B=0B = 0, the gcd is AA and the algorithm ends.
  3. Otherwise find qq and rr with A=qB+rA = qB + r and 0r<B0 \leq r < B. Here 0r<B<A0 \leq r < B < A and gcd(A,B)=gcd(B,r)\gcd(A,B) = \gcd(B,r). Replace AA by BB and BB by rr, then go to Step 2.

The original Euclidean algorithm uses subtraction instead of division. It rests on the observation that a common divisor of the positive integers AA and BB is also a common divisor of min(A,B)\min(A,B) and max(A,B)min(A,B)\max(A,B)-\min(A,B). So the gcd of two positive integers can be found like this.

  1. Let AA and BB be positive integers.
  2. If A=BA = B, the gcd is BB and the algorithm ends.
  3. Otherwise replace AA by max(A,B)min(A,B)\max(A,B)-\min(A,B) and BB by min(A,B)\min(A,B), then go to Step 2.

Starting from A=24A = 24 and B=15B = 15, the original algorithm runs as follows.

  1. A=2415=9A = 24-15 = 9, B=15B = 15
  2. A=159=6A = 15-9 = 6, B=9B = 9
  3. A=96=3A = 9-6 = 3, B=6B = 6
  4. A=63=3A = 6-3 = 3, B=3B = 3

That is, before reaching gcd(24,15)=3\gcd(24,15) = 3, it executes Step 3 four times.

Given two positive integers, count how many times the original Euclidean algorithm executes Step 3.

Input

The input consists of one line containing two positive integers separated by one or more spaces. Neither integer is larger than 32767.

Output

Print one line containing the number of times the original Euclidean algorithm executes Step 3.