#include <bits/stdc++.h>
int main(void) {
  unsigned long long a, b, count = 0, div;
  scanf("%I64u %I64u", &a, &b);
  while (a != 1 || b != 1) {
    if (a == 1 || b == 1) {
      count += (a + b - 1);
      a = b = 1;
    } else if (a >= b) {
      div = a / b;
      count += div;
      a -= b * div;
    } else {
      div = b / a;
      count += div;
      b -= a * div;
    }
  }
  if (!count) count++;
  printf("%I64u\n", count);
  return 0;
}
