#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000000 + 7;
long long power(long long x, long long y) {
  long long res = 1;
  while (y > 0) {
    if (y & 1) res = (res * x) % mod;
    y = y >> 1;
    x = (x * x) % mod;
  }
  return res;
}
void fun() {}
bool isPrime(long long n) {
  for (long long i = 2; i * i <= n; i++)
    if (n % i == 0) return false;
  return true;
}
const long long N = 1e6 + 7;
bool visited[N];
vector<long long> v[N];
long long a[N];
long long ans = 0, n, m, k;
long long x, y;
vector<long long> ve;
void dfs(long long node, long long root) {
  visited[node] = true;
  ve.push_back(node);
  for (auto it : v[node]) {
    if (!visited[it]) {
      dfs(it, root);
    }
  }
}
bool issubseq(string s, string t, long long m, long long n) {
  if (t.length() == 0) return false;
  if (m == 0) return true;
  if (s.length() > t.length()) return false;
  if (s[m - 1] == t[n - 1]) return issubseq(s, t, m - 1, n - 1);
  return issubseq(s, t, m, n - 1);
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  fun();
  long long tt = 1;
  while (tt--) {
    long long a, b;
    cin >> a >> b;
    long long ans = 0;
    while (a and b) {
      if (a < b) swap(a, b);
      ans += a / b;
      a %= b;
    }
    cout << ans;
  }
  return 0;
}
