#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }
long long solve(long long a, long long b) {
  if (a == b) return 1;
  if (a == 1) return b;
  if (b == 1) return a;
  long long d = gcd(a, b);
  a /= d;
  b /= d;
  long long val;
  if (a > b) {
    val = a / b;
    return val + solve(a - val * b, b);
  }
  val = b / a;
  return val + solve(a, b - val * a);
}
int main() {
  long long a, b;
  cin >> a >> b;
  cout << solve(a, b);
  return 0;
}
