#include <bits/stdc++.h>
using namespace std;
long long solve(long long a, long long b) {
  if (b == 0) return 0;
  long long t1 = max(a, b) / min(a, b);
  long long t2 = max(a, b) % min(a, b);
  return t1 * 3 + solve(b, t2);
}
int main() {
  long long N, X;
  cin >> N >> X;
  cout << solve(X, N - X) << endl;
  return 0;
}
