#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
ll N, X;
ll gcd(ll a, ll b) {
  ll r;
  r = a % b;
  if (r == 0)
    return b;
  else
    return gcd(b, r);
}
void solve() {
  ll q, p, s;
  ll sum = 0;
  while (1) {
    q = gcd(N, N - X);
    if (q == 0) {
      sum += 3 * X;
      break;
    } else if (X % q > 1 or (X > N - X)) {
      sum += 3 * (N - X);
      N = X;
      X = 2 * X - N;
    } else {
      p = X / q;
      s = (N - X) / X;
      sum += 3 * q * p + 3 * X * s;
      break;
    }
  }
  cout << sum << endl;
  return;
}
int main() {
  cin >> N >> X;
  solve();
  return 0;
}
