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