#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  unsigned long long n, x;
  cin >> n >> x;
  unsigned long long res = x, y = n - x;
  while (x != 0 && y != 0) {
    unsigned long long newy = y % x;
    if (y < x) swap(x, y);
    res += (y / x) * 2ULL + newy;
    y = newy;
  }
  cout << res << endl;
}
