#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, x;
  cin >> n >> x;
  if (x <= n / 2) {
    long long a = x;
    long long b = n - x;
    long long c = x;
    long long d = x;
    long long e = n - 2 * x;
    if (e) e = 2 * (e * (x / e)) - e;
    cout << a + b + c + d + e << endl;
  } else {
    int a = n - x;
    cout << 2 * (a * (x / a)) + x << endl;
  }
  return 0;
}
