#include <bits/stdc++.h>
using namespace std;
long long solve(long long N, long long X, long long tmp) {
  if (N - X <= 0) return 0;
  long long p;
  long long q;
  long long sum;
  sum = tmp;
  p = (N - X) / X;
  q = (N - X) % X;
  if (q == 0) {
    sum += 3 * p * X;
    return sum;
  } else {
    long long r;
    r = X / q;
    sum += 3 * p * X + 3 * q * r;
    sum += solve(X - r * q, q, sum);
    return sum;
  }
}
int main() {
  long long N, X;
  cin >> N >> X;
  long long ans;
  ans = solve(N, X, 0);
  cout << ans << endl;
  return 0;
}
