#include <bits/stdc++.h>
using namespace std;
const long long int INF = 1e15;
const long long int MOD = 1e9 + 7;
signed main() {
  long long int n, x;
  cin >> n >> x;
  long long int lMax = max(x, n - x);
  long long int lMin = min(x, n - x);
  cout << lMin * 3 * (lMax / lMin + 1) << endl;
  return 0;
}
