#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
const double pi = acos(-1.0);
long long x, y;
long long g(long long a, long long b) {
  if (a == 0 || b == 0) return 1;
  if (a > b)
    return g(a % b, b) + a / b;
  else
    return g(a, b % a) + b / a;
}
int main() {
  cin >> x >> y;
  cout << g(x, y) - 1;
  return 0;
}
