#include #include using namespace std; int M, N; void read() { cin >> M >> N; } int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } void work() { int div = gcd(M, N); M /= div; N /= div; int cnt = 0; while (M > 1 || N > 1) { if (M > N) { cnt += N == 1 ? M - 1 : M / N; M %= N; } else { ++cnt; swap(M, N); } } cout << cnt << endl; } int main() { read(); work(); return 0; }