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