#include <bits/stdc++.h>
using namespace std;
const int N = 2000100;
const int mod = (int)pow(10, 9) + 7;
const long long INF = 1LL << 60;
long long n, x, ans = 1, h, w;
pair<long long, long long> p;
int main() {
  cin >> n >> x;
  ans += n;
  w = x, h = n - x;
  while (!(h == 1 && w == 1)) {
    ans += 2 * min(h, w);
    if (h > w)
      h -= w;
    else
      w -= h;
  }
  cout << ans << endl;
  return 0;
}
