#include <bits/stdc++.h>
using namespace std;
long long a, b;
long long ans;
long long gcd(long long a, long long b) {
  long long temp;
  while (b != 0) {
    temp = a % b;
    a = b;
    b = temp;
  }
  return a;
}
int main() {
  scanf("%I64d%I64d", &a, &b);
  ans = 0;
  while (a != 1 || b != 1) {
    if (a > b) {
      ans += (a - 1) / b;
      a = (a - 1) % b + 1;
    } else {
      ans += (b - 1) / a;
      b = (b - 1) % a + 1;
    }
    long long temp = gcd(a, b);
    a /= temp;
    b /= temp;
  }
  printf("%I64d\n", ans + 1);
  return 0;
}
