#include <bits/stdc++.h>
using namespace std;
template <class pair<pair<int, int>, int> >
void chmin(pair<pair<int, int>, int>& a, const pair<pair<int, int>, int>& b) {
  if (a > b) a = b;
} template <class pair<pair<int, int>, int> >
void chmax(pair<pair<int, int>, int>& a, const pair<pair<int, int>, int>& b) {
  if (a < b) a = b;
} long long N, X;
long long ans = 0;
void rec(long long a, long long b) {
  cout << a << ' ' << b << endl;
  if (a < b) {
    rec(b, a);
    return;
  }
  if (a % b == 0ll) {
    ans += (2ll * (a / b) - 1ll) * b;
    return;
  }
  ans += 2LL * (a / b) * b;
  rec(b, a % b);
}
int main() {
  cin >> N >> X;
  ans = N;
  rec(X, N - X);
  cout << ans << endl;
  return 0;
}
