#include <bits/stdc++.h>
using namespace std;
int dx4[4] = {1, 0, -1, 0};
int dy4[4] = {0, 1, 0, -1};
int dx8[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int main(void) {
  long long N, X;
  cin >> N >> X;
  long long ans = N;
  long long nokori = X;
  if (2 * X <= N) {
    ans += 2 * X;
    ans += (nokori / (N - 2 * X) * 2 - 1) * (N - 2 * X);
  } else
    ans += (nokori / (N - X) * 2 - 1) * (N - X);
  cout << ans << endl;
}
