from fractions import gcd M,N=map(int,raw_input().split()) g=gcd(M,N);M,N=M/g,N/g;a=-1 if M>=N:a-=1;M,N=N,M while M^0:M,N=N,M;a+=1+M/N;M%=N print a