#include <bits/stdc++.h>
using namespace std;
const long long int INF = 3e18;
const double PI = acos(-1);
long long int n, x;
long long int ans = 0;
void ugrid(long long int a, long long int b) {
  if (a % b == 0) {
    ans += 2 * b + 1;
  } else {
    ans += n;
    ugrid(b, a % b);
  }
}
signed main() {
  cin >> n >> x;
  ans += 2 * x;
  ugrid(max(x, n - x), min(x, n - x));
  cout << ans << endl;
  return 0;
}
