#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll n, x;
  ll num;
  ll ans;
  cin >> n >> x;
  num = max(x, n - x);
  if (n % 2 == 0) {
    ans = 3 * (n - num);
  } else {
    num = min(x, n - x);
    if ((n - num) % (num) == 0 && num > 1) {
      ans = 3 * (n - num);
    } else {
      ans = 3 * num + 3 * (n - num - 1);
    }
  }
  cout << ans << endl;
  return 0;
}
