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