#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const long long INF = 1000000009;
long long L(long long a, long long b) {
  long long tmp = 0;
  if (a > b) swap(a, b);
  if (a == b && a == 1) return 0;
  tmp += 2 * a + L(b - a, a);
}
signed main() {
  long long n, x;
  cin >> n >> x;
  long long ans = 0;
  ans += n;
  if (x < n - x)
    ans += L(x, n - x);
  else
    ans += L(n - x, x);
  cout << ans + 1 << endl;
  return 0;
}
