#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, x, sum = 0;
  cin >> n >> x;
  long long int mx, mn;
  mx = max(n - x, x);
  mn = n - mx;
  if (!(mx % mn)) {
    sum += mx + 2 * (mx / mn) * mn;
  } else {
    sum += n + 2 * (mx / mn) * mn + (1 + 2 * (mn - 1));
  }
  cout << sum << endl;
  return 0;
}
