#include <bits/stdc++.h>
using namespace std;
static const double PI = 3.14159265358979323846264338327950288419716939937511;
long long gcdRet(long long a, long long b) {
  long long ret = 0;
  while (a && b) {
    if (a > b) {
      ret += (a / b);
      a %= b;
    } else {
      ret += b / a;
      b %= a;
    }
  }
  return ret;
}
long long gcd(long long a, long long b) {
  for (;;) {
    if (a == 0) return b;
    b %= a;
    if (b == 0) return a;
    a %= b;
  }
}
int main() {
  long long a, b;
  cin >> a >> b;
  long long ret = 0, g = gcd(a, b);
  ret += gcdRet(a, b);
  cout << ret << endl;
}
