#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int N, X;
  scanf("%lld %lld", &N, &X);
  long long int Suma = 0;
  long long int Bx, By;
  Suma += X;
  Bx = X;
  By = N - X;
  Suma += By;
  while (Bx % By != 0) {
    int Path = Bx / By;
    Suma += By * (2 * Path);
    int Res = Bx % By;
    Suma += 2 * Res;
    Bx = Res;
    By = By - Res;
  }
  Suma += By * (2 * Bx / By - 1);
  cout << Suma;
  return 0;
}
