#include <bits/stdc++.h>
int main(void) {
  long long int N, X;
  std::cin >> N >> X;
  long long int a[2];
  a[0] = X;
  a[1] = N;
  long long int dist = 0;
  while (a[0] != a[1]) {
    long long int temp = (a[1] - 1) / a[0];
    if (temp > 1) {
      std::cout << temp << std::endl;
      a[1] -= a[0] * (temp - 1);
      dist += a[0] * 3 * (temp - 1);
    }
    a[1] -= a[0];
    std::sort(a, a + 2);
    dist += a[0] * 3;
  }
  std::cout << dist << std::endl;
}
