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