import java.util.*; public class Main { private static Scanner sc = new Scanner(System.in); public static void main(String[] args) throws Exception { int m = sc.nextInt(); int n = sc.nextInt(); int d = gcd(m,n); m /= d; n /= d; int ret = 0; while (true) { if (n == 1) { ret += m-1; break; } if (m < 0) { ret = -1; break; } if (m == n) break; if (m > n) { m -= n; ret++; } else { int tmp = m; m = n; n = tmp; ret++; } } System.out.println(ret); } public static int gcd(int a, int b) { if (b == 0) return a; int tmp = a; a = b; b = tmp%b; return gcd(a, b); } }