#include <bits/stdc++.h>
using namespace std;
long long n, x;
long long a, b;
int main() {
  long long ans = 0;
  cin >> n >> x;
  a = x, b = n - x;
  long long ap, bp;
  long long i = 0;
  while (true) {
    if (i % 2 == 0) ans += a;
    i++;
    ans += (a / b) * 2 * b;
    ap = b;
    bp = a % b;
    if (bp == 0) break;
    a = ap;
    b = bp;
  }
  cout << ans << '\n';
  return 0;
}
