#include <bits/stdc++.h>
using namespace std;
int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
void fast() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
}
void solve() {
  long long a, b;
  cin >> a >> b;
  long long res = 0;
  while (a && b) {
    if (a < b) {
      a = b - a;
      b -= a;
      res++;
    } else {
      res += a / b;
      a %= b;
    }
  }
  cout << res << "\n";
}
int main() {
  fast();
  solve();
}
