#include #include #include #include #include #include #include #include #include #include #include #include #include #include #define INF 1000000000 using namespace std; typedef long long ll; int main(void) { ll M, N; cin >> M >> N; for (int i = 2; i*i <= N; i++) { if (M % i == 0 && N % i == 0) { M /= i; N /= i; } } ll num = 0; while (1) { if (M == N) { if (M == 1) break; cout << -1 << endl; return 0; } if (M > N) { ll k = M/N; if (k != 1) { M -= (k-1)*N; num += k-1; } else { M -= N; num++; } } else { swap(M, N); num++; } } cout << num << endl; return 0; }