#include <bits/stdc++.h>
using namespace std;
long long res(long long a, long long b) {
  if (!a) return 0;
  long long ret = a / b;
  a = a % b;
  if (a) ret += res(b, a);
  return ret;
}
int main() {
  long long i, j, k, l, temp, a, b, t, n, ans, caseno = 0;
  string str;
  while (cin >> a >> b) {
    cout << res(a, b) << endl;
  }
  return 0;
}
