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