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