#include <bits/stdc++.h>
using namespace std;
struct INIT {
  INIT() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} init;
int main() {
  long long N, X;
  cin >> N >> X;
  long long res = N;
  long long R = N - X, C = X;
  while (R > 0 && C > 0) {
    if (R > C) {
      res += (R / C) * C * 2;
      R %= C;
      C -= R;
      res += R;
    } else {
      res += (C / R) * R * 2;
      C %= R;
      R -= C;
      res += C;
    }
  }
  cout << res << endl;
  return 0;
}
