#include <bits/stdc++.h>
using namespace std;
long long gcd(long long, long long);
int main(void) {
  long long N, X;
  cin >> N >> X;
  if (X > N / 2) X = N - X;
  long long L = N;
  long long tmp;
  N -= X;
  while (X) {
    if (N % X > 0) {
      L += 2 * (N / X) * X;
    } else {
      L += 2 * (N / X) * X - X;
      break;
    }
    tmp = X;
    X = gcd(N, X);
    N = tmp;
  }
  cout << L << endl;
  return 0;
}
long long gcd(long long m, long long n) {
  long long tmp;
  while (n) {
    if (m >= n) {
      tmp = m % n;
    } else {
      tmp = m;
    }
    m = n;
    n = tmp;
  }
  return m;
}
