#include <bits/stdc++.h>
long long rnp(long long a, long long b) {
  if (a == 0 || b == 0) return 0;
  if (a == 1 && b == 1) return 1;
  if (a > b) {
    return rnp(a % b, b) + a / b;
  } else {
    return rnp(a, b % a) + b / a;
  }
}
unsigned long long resistor_number(unsigned long long a, unsigned long long b) {
  unsigned long long a_curr = 1, b_curr = 1;
  unsigned long long n = 1;
  while (a != a_curr && b != b_curr) {
    if (a_curr / (b_curr * 1.) > a / (b * 1.)) {
      a_curr = b_curr;
      b_curr += a_curr;
      ++n;
    } else {
      a_curr += b_curr;
      ++n;
    }
  }
  return n;
}
int main() {
  long long a, b;
  std::cin >> a >> b;
  std::cout << rnp(a, b);
  return 0;
}
