#include <bits/stdc++.h>
using namespace std;
int GLL(long long& x) { return scanf("%lld", &x); }
int GI(int& x) { return scanf("%d", &x); }
int n, x;
int main() {
  GI(n);
  GI(x);
  long long res = n;
  long long a = x;
  long long b = n - x;
  if (a > b) swap(a, b);
  while (a > 0) {
    long long q = b / a;
    long long r = b % a;
    if (r > 0) {
      res += 2 * q * a;
    } else {
      res += (2 * q - 1) * a;
    }
    b = a;
    a = r;
  }
  cout << res << "\n";
  return 0;
}
