#include <bits/stdc++.h>
using namespace std;
int main() {
  long long a, b, k = 0, t;
  cin >> a;
  cin >> b;
  if (a == b || b == 1) {
    cout << (long long)a / b;
    return 0;
  }
  if (a > b) {
    k = (long long)a / b;
    a = a - k * b;
  }
  if (a < b)
    while (a != 1) {
      k += (long long)b / a;
      t = a;
      a = b - ((long long)b / a) * a;
      b = t;
    }
  k += b;
  cout << k;
  return 0;
}
