#include <bits/stdc++.h>
using namespace std;
class XY {
 public:
  long long x, y;
  XY() { x = y = 0; }
  XY(long long u, long long v) {
    x = u;
    y = v;
  }
};
template <typename T1, typename T2>
void chmin(T1 &a, T2 b) {
  if (a > b) a = b;
}
template <typename T1, typename T2>
void chmax(T1 &a, T2 b) {
  if (a < b) a = b;
}
template <typename T1, typename T2>
long long mypow(T1 a, T2 n) {
  if (n == 0) return 1;
  if (n == 1) return a;
  if (n % 2) return a * mypow(a, n - 1);
  long long tmp = mypow(a, n / 2);
  return tmp * tmp;
}
template <typename T>
int BS(vector<T> V, int left, int right, T key) {
  int mid = (left + right) / 2;
  if (V[mid] <= key)
    left = mid;
  else
    right = mid;
  if (right - mid == 1)
    return left;
  else
    return BS(V, left, right, key);
}
long long comb(long long n, long long r) {
  long long res = 1;
  for (long long i = 0; i < r; i++) {
    res *= n - i;
    res /= i + 1;
  }
  return res;
}
template <typename T>
T euclid(T a, T b) {
  if (b == 0)
    return a;
  else
    return euclid(b, a % b);
}
int rec(long long a, long long b) {
  if (a > b) {
    long long tmp = a;
    a = b;
    b = tmp;
  }
  if (b % a == 0) return (2 * (b / a) - 1) * a;
  return 2 * (b / a) * a + rec(a, b % a);
}
void Main() {
  long long N, X;
  cin >> N >> X;
  cout << N + rec(X, N - X) << endl;
}
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << std::fixed << std::setprecision(15);
  Main();
}
