#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-7;
const int inf = 0x3f3f3f3f;
inline long long read() {
  long long f = 1, x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + (c ^ 48);
    c = getchar();
  }
  return f * x;
}
const int mod = 1e9 + 7, N = 1e5 + 10;
int main() {
  long long n, x;
  while (scanf("%lld %lld", &n, &x) != EOF) {
    if (x <= (n / 2)) {
      if (2 * x == n) {
        printf("%lld\n", 3 * x);
      } else {
        long long a = n - 2 * x, b = x, ans = 2 * x + n;
        while (b % a) {
          ans += 2 * a * (b / a);
          ans += 2 * (b - a * (b / a));
          long long tempa = a, tempb = b;
          b = tempb - tempa * (tempb / tempa);
          a = tempa - (tempb - tempa * (tempb / tempa));
        }
        ans += 2 * b - a;
        printf("%lld\n", ans);
      }
    } else {
      long long ans = n, b = x, a = n - x;
      while (b % a) {
        ans += 2 * (b / a) * a;
        ans += 2 * (b - a * (b / a));
        long long tempa = a, tempb = b;
        b = tempb - tempa * (tempb / tempa);
        a = tempa - (tempb - tempa * (tempb / tempa));
      }
      ans += 2 * b - a;
      printf("%lld\n", ans);
    }
  }
  return 0;
}
