#include <bits/stdc++.h>
using namespace std;
long long int gcm(long long a, long long b) {
  long long r, tmp;
  if (a < b) {
    tmp = a;
    a = b;
    b = tmp;
  }
  r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}
int main() {
  long long N, X, ans;
  cin >> N >> X;
  if (X > N / 2) X = N - X;
  long long g = gcm(N, X);
  N /= g;
  X /= g;
  ans = ((N - X) / X) * 3 * X;
  if ((N - X) % X > 0) ans += 3 * X;
  ans *= g;
  cout << ans << endl;
  return 0;
}
