#include <bits/stdc++.h>
using namespace std;
int main(int argc, char* argv[]) {
  int N, X;
  cin >> N >> X;
  int ans;
  if (X * 3 < N) {
    int num = (N - X) / X;
    ans = num * 2 * X + N - X;
  } else {
    int num = X / (N - X - X);
    ans = X + N - X + X + X + (num * 2 - 1) * (N - X - X);
  }
  cout << ans << endl;
  return 0;
}
