#include <bits/stdc++.h>
int main(void) {
  int n, x;
  scanf("%d %d", &n, &x);
  long long counter = 0;
  int len1, len2;
  counter += n;
  len1 = x;
  len2 = n - x;
  while (1) {
    if (len1 < len2) {
      if (len2 % len1 == 0) {
        counter += (len2 / len1) * 2 * len1 - len1;
        break;
      } else {
        counter += (len2 / len1) * 2 * len1;
        len2 -= (len2 / len1) * len1;
      }
    } else if (len1 > len2) {
      if (len1 % len2 == 0) {
        counter += (len1 / len2) * 2 * len2 - len2;
        break;
      } else {
        counter += (len1 / len2) * 2 * len2;
        len1 -= (len1 / len2) * len2;
      }
    } else {
      counter += len2;
      break;
    }
  }
  printf("%lld\n", counter);
  return 0;
}
