#include <bits/stdc++.h>
using namespace std;
long long sum, x, n;
void gcd(long long x, long long y) {
  if (y == 0) {
    sum -= x;
    return;
  }
  sum += x / y * y * 2;
  gcd(y, x % y);
}
int main() {
  scanf("%lld%lld", &n, &x);
  gcd(x, n - x);
  cout << sum + n << endl;
}
