from math import gcd N,M=map(int,input().split()) G=gcd(N,M) N//=G M//=G ANS=0 while N!=1 or M!=1: #print(N,M) if M>N: M,N=N,M ANS+=1 else: if N%M==0: ANS+=N//M-1 N,M=1,1 else: ANS+=N//M N%=M print(ANS)