#include <bits/stdc++.h>
int comp(const void *x, const void *y) { return *(int *)x - *(int *)y; }
int min(int x, int y) { return (x < y) ? x : y; }
int solve(unsigned long long x, unsigned long long y) {
  if (x > y) {
    return (x / y) * 2 + solve(x % y, y);
  } else if (x == 0) {
    return 0;
  } else {
    unsigned long long s = y / x;
    return s * (x + y) - (s * (s - 1) / 2) * x + solve(y % x, x);
  }
}
int main(void) {
  unsigned long long n, x;
  scanf("%llu%llu", &n, &x);
  printf("%llu\n", x + solve(x, n - x));
  return 0;
}
