import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner koko = new Scanner(System.in); int m = koko.nextInt(); int n = koko.nextInt(); int mm = m/gcd(m, n); int nn = n/gcd(m, n); long ans = keisan(mm, nn, 0); System.out.println(ans); } static int gcd(int a, int b){ if(b==0){ return a; }else{ return gcd(b, a%b); } } static long keisan(int m, int n, long count){ if(m==n){ return count; }else if(n==1){ return count+m-1; } else if(m>n){ int amari=m%n; int a = (m-amari)/n; return keisan(amari, n, count+a); }else{ return keisan(n, m, count+1); } } }