#include <bits/stdc++.h>
#pragma GCC target( \
    "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("fast-math")
using namespace std;
const long double EPS = 1e-9;
const long double PI = acos(-1);
int mod = (int)998244353;
const int MOD7 = 1000000007;
const int MOD9 = 1000000009;
const int a228 = 18;
const int inf = 1e9;
const long long kekmod = 1791791791;
const long long bestmod = 1148822869;
const long long secmod = (int)1e9 + 113;
vector<long long> mods = {kekmod, bestmod, mod, MOD9, 1000000007};
vector<long long> hashpows = {29, 31, 37, 43, 47, 53, 179, 229};
signed main() {
  ios_base::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);
  srand(time(NULL));
  long long n, x;
  cin >> n >> x;
  if (n % 2 == 0 && x == n / 2) {
    cout << x * 3;
    return 0;
  }
  x = min(x, n - x);
  if (n % x == 0 && x > 1) {
    cout << (n - x) * 3;
    return 0;
  }
  cout << (n - 1) * 3;
}
