#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7, INF = 1e9;
using vi = vector<long long>;
using vvi = vector<vi>;
signed main() {
  long long n, x;
  cin >> n >> x;
  long long va = 0;
  long long i = 1;
  do {
    i++;
    if (x % i == 0) {
      va = i;
      break;
    }
  } while (i * i < n);
  if (x > n - x) x = n - x;
  if ((double)n / 2 == x)
    cout << x * 3 << endl;
  else if (va != 0 && n % va == 0)
    cout << n + n - va + n - va - va << endl;
  else
    cout << n + n - 1 + n - 2 << endl;
}
