#include <bits/stdc++.h>
using namespace std;
int main(void) {
  bool isDaikei = true;
  unsigned long side1, side2;
  unsigned long n, x;
  unsigned long length = 0;
  scanf("%d", &n);
  scanf("%d", &x);
  if (x >= side2) {
    side1 = x;
    side2 = n - x;
  } else {
    side1 = n - x;
    side2 = x;
  }
  length = n;
  unsigned long div, r;
  r = 1;
  while (r != 0) {
    div = side1 / side2;
    r = side1 % side2;
    if (r != 0) {
      length += div * 2 * side2;
    } else {
      length += (2 * div - 1) * side2;
    }
    side1 = side2;
    side2 = r;
  }
  printf("%d\n", length);
  return 0;
}
