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