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