#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int way(ll a, ll b) {
  if (a == b) return a;
  ll ret;
  if (2 * a < b)
    ret = 2 * a + way(a, b - a);
  else
    ret = 2 * a + way(b - a, a);
  return ret;
}
int main(void) {
  ll N, X;
  cin >> N >> X;
  if (X < N - X) {
    cout << N + way(X, N - X) << endl;
  } else {
    cout << N + way(N - X, X) << endl;
  }
}
