#include <bits/stdc++.h>
long long int solve(long long int x, long long int y) {
  printf("%lld %lld\n", x, y);
  if (x > y) std::swap(x, y);
  if (x == 0) return 0;
  return (y / x + 1) * x + solve(x, y % x);
}
int main() {
  long long int n, x;
  scanf("%lld%lld", &n, &x);
  printf("%lld\n", n + solve(x, n - x));
  return 0;
}
