import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int M = sc.nextInt(); int N = sc.nextInt(); int div = getCommonDivisor(M, N); int m = M/=div; int n = N/=div; int ans = 0; while(true) { if(m==1) break; if(m>n) { m-=n; ans++; } else { int tmp=m; m=n; n=tmp; ans++; } } ans++; ans += (n-1); System.out.println(ans); } static int getCommonDivisor(final int a, final int b) { int nowA = a; int nowB = b; while(true) { int tmp = nowB % nowA; if(tmp == 0) break; nowB = nowA; nowA = tmp; } return nowA; } }