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