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