#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, x;
  cin >> n >> x;
  ll ans = n;
  ll a = x, b = n - x;
  while (1) {
    if (b % a == 0) {
      ans += (b / a) * 2 * a - a;
      break;
    }
    if (a < b) {
      ans += (b / a) * 2 * a;
      ll tmpa = b % a, tmpb = a;
      a = tmpa;
      b = tmpb;
    } else {
      ans += 2 * (a / b) * b;
      a = a % b;
    }
  }
  cout << ans << endl;
  return 0;
}
