#include <bits/stdc++.h>
int main() {
  uint32_t N, X;
  uint32_t base, l, tmp;
  uint32_t ans;
  scanf("%u %u", &N, &X);
  ans = N;
  base = N - X;
  l = X;
  while (l) {
    ans += ((base - 1) / l) * (2 * l);
    tmp = base;
    base = l;
    if ((tmp % l) == 0) {
      ans += l;
    }
    l = tmp % l;
  }
  printf("%u\n", ans);
}
