#include <bits/stdc++.h>
long N;
long X;
long ans = 0;
void solve(long n, long x, int f) {
  long d = n / x;
  long m = n % x;
  if (m > 0) {
    if (f)
      ans += (d - 1) * 2 * x;
    else
      ans += d * 2 * x;
    solve(x, m, 0);
  } else {
    ans += d * 2 * x - x;
    return;
  }
}
int main() {
  long i, j;
  scanf("%ld %ld", &N, &X);
  if (N % 2 && N / 2 == X) {
    ans = N / 2 * 3;
  } else if (N % X == 0) {
    ans = (N / X - 1) * 2 * X + N - X;
  } else {
    solve(N, X, 1);
    ans += N;
  }
  printf("%ld\n", ans);
  return 0;
}
