#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
int n, x;
ll len(ll a, ll b) {
  if (b == 0) return -a;
  return len(b, a % b) + (a / b) * (2 * b);
}
int main() {
  cin >> n >> x;
  ll ans = x + (n - x);
  ans += len(max(x, n - x), min(x, n - x));
  cout << ans << endl;
}
