#include <bits/stdc++.h>
using namespace std;
unsigned long long int calc(unsigned long long int a, unsigned long long int b,
                            unsigned long long int count) {
  if (a == 0) return count;
  if (a > b) {
    count = count + a / b;
    return (calc(a % b, b, count));
  }
  return (calc(b - a, a, count + 1));
}
int main() {
  unsigned long long int a, b;
  cin >> a >> b;
  unsigned long long int count = 0;
  cout << calc(a, b, count);
}
