#include <bits/stdc++.h>
using namespace std;
using llong = long long;
llong length(llong K, llong X, llong L) {
  return K == X ? (K + L)
                : (K < X ? length(K, X - K, L + 2 * K)
                         : length(K - X, X, L + 2 * X));
}
int main() {
  llong N, X;
  cin >> N >> X;
  cout << N + length(N - X, X, 0) << endl;
}
