#include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T>
inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T>
inline T sqr(T x) {
  return x * x;
}
int main(void) {
  long long N, X;
  cin >> N >> X;
  long long ans = X, ulim[3], now = X, edge = 0, next, dir, dist = X, nnow;
  ulim[0] = X;
  ulim[1] = 0;
  ulim[2] = 0;
  do {
    next = (edge + 1) % 3;
    if (N - now > ulim[next]) {
      ulim[edge] = max(ulim[edge], now);
      nnow = N - now;
      edge = next;
      dist = now - ulim[next];
      ans += dist;
      now = nnow;
    } else {
      dir = (edge + 2) % 3;
      dist = N - ulim[next];
      nnow = 2 * N - now - ulim[next];
      dist = now - ulim[next];
      ans += dist;
      now = nnow;
      edge = dir;
    }
  } while (!(now == N - X && edge == 2));
  cout << ans << endl;
  return 0;
}
