#include <bits/stdc++.h>
using namespace std;
template <typename T>
T gcd(T a, T b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
vector<long long> b_v;
void bin(unsigned long long n) {
  if (n > 1) bin(n >> 1);
  b_v.emplace_back(n & 1);
}
long long isPowerof2(long long x) { return (x && !(x & x - 1)); }
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ostream_iterator<long long> output(cout, " ");
  long long a, b;
  cin >> a >> b;
  long long ans = 0;
  while (1) {
    if (a == 1 || b == 1) {
      ans += max(a, b);
      break;
    } else {
      if (a < b) {
        ans += b / a;
        b = b % a;
      } else if (a > b) {
        ans += a / b;
        a = a % b;
      } else {
        a = 1;
        b = 1;
      }
    }
  }
  cout << ans;
  return 0;
}
