#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, x;
  cin >> n >> x;
  long long ans = n;
  long long noko = n - x;
  long long a = x;
  while (n % a != 0) {
    ans += noko / a * 2 * a;
    long long tmp = noko;
    noko = a;
    a = tmp % a;
  }
  ans += noko / a * 2 * a - a;
  cout << ans << endl;
  return 0;
}
