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