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