#include <bits/stdc++.h>
using namespace std;
struct timer {
  time_t start;
  timer() { start = clock(); }
  ~timer() {
    cerr << 1. * (clock() - start) / CLOCKS_PER_SEC << " secs" << endl;
  }
};
long long a, b;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long f(long long a, long long b) {
  if (b == 1) return a;
  return a / b + f(b, a % b);
}
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cin >> a >> b;
  long long g = gcd(a, b);
  a /= g, b /= g;
  cout << (a > b ? f(a, b) : f(b, a)) << endl;
}
