#include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << "\n";
  err(++it, args...);
}
long long ans = 0;
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else {
    ans += a / b;
    return gcd(b, a % b);
  }
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  long long n, m;
  cin >> n >> m;
  gcd(n, m);
  cout << ans;
}
