#include <bits/stdc++.h>
int main(void) {
  int n, x, a, b, sum, tmp, k;
  scanf("%d %d", &n, &x);
  sum = n;
  a = ((x) > (n - x) ? (x) : (n - x));
  b = ((x) < (n - x) ? (x) : (n - x));
  while (tmp) {
    k = a / b;
    tmp = a % b;
    if (tmp == 0) {
      sum += (k - 1) * b * 2 + b;
    } else {
      sum += k * b * 2;
      a = b;
      b = tmp;
    }
  }
  printf("%d", sum);
  return 0;
}
