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