#include <bits/stdc++.h>
using namespace std;
int main() {
  long long sum, a, b, t;
  while (cin >> a >> b) {
    sum = 0;
    t = a % b;
    if (t == 0) {
      cout << a / b << endl;
      continue;
    }
    while (t != 1) {
      sum += a / b;
      a = b;
      b = t;
      if (b == 0) break;
      t = a % b;
    }
    sum += b;
    if (a > b && b != 0) sum += a / b;
    cout << sum << endl;
  }
}
