#include <bits/stdc++.h>
int calc(int a, int b, int sum) {
  if (!a) return sum;
  return calc(b % a, a, sum + 2 * a * (b / a));
}
int main() {
  int n, x;
  scanf("%d%d", &n, &x);
  printf("%d\n", calc(n, x, 0));
  return 0;
}
