#include <bits/stdc++.h>
using namespace std;
const long long nmax = 1e9 + 7;
const long long mod = 998244353;
const long long INF = 1e18;
const double PI = 2 * asin(1);
long long gcd(long long A, long long B) {
  long long M = max(A, B), m = min(A, B);
  while (M % m != 0) {
    long long c = M % m;
    M = m;
    m = c;
  }
  return m;
}
int main() {
  long long N, X;
  cin >> N >> X;
  if (gcd(N, X) == 1) {
    cout << 3 * (N - 1) << endl;
  } else {
    long long now = min(X, N - X);
    cout << 3 * (N - now) << endl;
  }
  return 0;
}
