#include <bits/stdc++.h>
using namespace std;
using LL = long long;
LL solve(LL N, LL X) {
  if (2 * X == N) return 3 * X;
  if (2 * X < N) {
    auto A = N - X;
    auto B = A % X;
    return X * (2 * (A / X) + 1) + A + B * (2 * (X / B) - 1);
  } else {
    auto A = N - X;
    auto B = X % A;
    return X + A * (2 * (X / A) + 1) + B * (2 * (A / B) - 1);
  }
}
int main() {
  LL N, X;
  cin >> N >> X;
  cout << solve(N, X) << endl;
}
