#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const long long INF = 1e+14;
const long double eps = 1e-8;
const long double pi = acos(-1.0);
void Yes() {
  cout << "Yes" << endl;
  exit(0);
}
void No() {
  cout << "No" << endl;
  exit(0);
}
long long light(long long x, long long y) {
  if (x == y)
    return x;
  else if (x > y) {
    if (x % y != 0) return 2 * y * (long long)(x / y) + light(y, x % y);
    return 2 * y * (long long)(x / y) - y;
  } else {
    if (y % x != 0) return 2 * x * (long long)(y / x) + light(x, y % x);
    return 2 * y * (long long)(x / y) - x;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long N, X;
  cin >> N >> X;
  cout << N + light(X, N - X) << endl;
  return 0;
}
