#include <bits/stdc++.h>
using namespace std;
long long n, k;
int main() {
  scanf("%lld%lld", &n, &k);
  if (k * 2LL == n) {
    printf("%lld", 3LL * k);
  } else {
    long long res = n;
    long long x = min(k, n - k), y = n - x;
    while (x != 0LL) {
      if (x >= y) break;
      res += 2 * x;
      res += (x - 1LL) / (y - x) * 2LL * (y - x);
      long long r = y - x;
      x %= r;
      y = r;
    }
    res += y;
    printf("%lld", res);
  }
  return 0;
}
