#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll N, x;

ll gcd(ll x, ll y) {
  if(y == 0) return x;

  return gcd(y, x % y);
}

int main() {
  cin >> N >> x;

  cout << 3 * (N - gcd(N,x)) << endl;
}
