#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, x, s, t, tt;
  cin >> n >> x;
  s = n;
  t = n - x;
  while (t >= x && x > 0) {
    s += 2 * (t / x) * x;
    tt = t % x;
    t = x;
    x = tt;
  }
  cout << s - t << endl;
  return 0;
}
