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(); if(M == N) { System.out.println(0); return; } int div = getCommonDivisor(M, N); int m = M/=div; int n = N/=div; int ans = 0; while(true) { if(m==1 || n==1) break; if(m>n) { m-=n; ans++; } else { int tmp=m; m=n; n=tmp; ans++; } } if(m==1) { ans++; ans += (n-1); } else if(n==1) { ans += (m-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; } }