#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, x;
  cin >> n >> x;
  long long a = x, b = 0;
  long long turn = 1;
  long long ret = 0;
  long long top = x;
  long long bottom = n;
  long long left = x;
  while (true) {
    long long height = bottom - top;
    if (b == 0) {
      ret += left;
      b = left;
    } else if (a == top) {
      if (height != 1) {
        left = a % height;
        ret += 2 * height * (a / height) + height;
        a = bottom;
      } else {
        ret += 2 * b;
        a = x;
        b = 0;
      }
    } else if (a == bottom) {
      if (b > height) {
        ret += height;
        left -= height;
        a -= height;
        b -= height;
      } else {
        ret += b;
        a = a - b;
        b = 0;
        bottom = a;
      }
    }
    {};
    {};
    if (a == x && b == 0) break;
    turn++;
  }
  std::cerr << "[ answer ]: ";
  cout << (ret) << endl;
END:
  return 0;
}
