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