#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int MOD = 1e9 + 7;
void solve() {
  long long n, m, i, j, k, l, r, x, y, z, a, b, c;
  cin >> a >> b;
  long long ans = 0;
  while (b) {
    ans += a / b;
    a %= b;
    swap(a, b);
  }
  cout << ans << "\n";
}
int main() {
  int t;
  solve();
}
