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