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