#include <bits/stdc++.h>
using namespace std;
long long n, x;
long long ans;
int main() {
  freopen("AGC001_2.in", "r", stdin);
  freopen("AGC001_2.out", "w", stdout);
  scanf("%lld %lld", &n, &x);
  ans = n;
  long long a = x, b = n - x, c;
  while (a != 1) {
    if (a > b) swap(a, b);
    c = a, a = b - a, b = c;
    ans += 2 * a;
  }
  ans += b * 2 - 1;
  printf("%lld\n", ans);
  return 0;
}
