#include <bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[]) {
  long long N, X, ans = 0;
  cin >> N >> X;
  if (X * 2 == N) {
    ans = X * 3;
  } else {
    long long Y = N - X;
    if ((X % Y) == 0 || (Y % X) == 0) {
      ans = max(X, Y) * 3;
    } else {
      ans = (N - 1) * 3;
    }
  }
  cout << ans << endl;
  return 0;
}
