#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  long long a, b;
  cin >> a >> b;
  long long ret = 0;
  while (a > 0) {
    if (a >= b) {
      long long c = max(1LL, (a - b) / b);
      ret += c;
      a -= c * b;
    } else {
      long long c = max(1LL, (b - a) / a);
      ret += c;
      b -= c * a;
    }
  }
  cout << ret << endl;
  return 0;
}
