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