#include <bits/stdc++.h>
using namespace std;
long long n, x;
long long ans = 0;
int main() {
  scanf("%lld %lld", &n, &x);
  long long a, b;
  a = min(x, n - x);
  b = max(x, n - x);
  while (a) {
    if (b % a != 0) {
      ans += (int)(b / a) * a * 2;
      b = b % a;
      swap(a, b);
    } else {
      ans += (int)(b / a) * a * 2 - a;
      break;
    }
  }
  printf("%lld\n", ans + n);
  return 0;
}
