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