#include <bits/stdc++.h>
using namespace std;
long long int N, X;
long long int a, b, c;
long long int sum;
int main() {
  cin >> N >> X;
  a = min(X, N - X);
  b = max(X, N - X);
  sum = N;
  while (b % a != 0) {
    sum += (b / a) * 2 * a;
    c = a;
    a = b % a;
    b = c;
  }
  sum += (b / a + 1) * a;
  cout << sum << endl;
}
