#include <bits/stdc++.h>
using namespace std;
const long long LLINF = (1LL << 60);
const long long INF = (1LL << 30);
const long long MOD = 1000000007;
long long N, X;
signed main() {
  cin >> N >> X;
  long long a = min(X, N - X);
  long long b = max(X, N - X);
  long long ans = 0;
  while (b % a != 0) {
    ans += (b / a) * 2 * a + a + b;
    a = b % a;
    b = a;
  }
  ans += b + (b / a) * 2 * a;
  printf("%lld\n", ans);
  return 0;
}
