#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, x;
  cin >> n >> x;
  long long ans = n;
  long long a = n - x, b = x;
  if (a < b) swap(a, b);
  while (b != 0) {
    long long k = a / b;
    ans += k * b * 2;
    long long tmp = a % b;
    a = b;
    b = tmp;
  }
  cout << ans - 1 << endl;
  return 0;
}
