#include <bits/stdc++.h>
using namespace std;
long long n, x, ans;
void solve(long long a, long long b) {
  long long tmp = 2LL * (a / b);
  ans += tmp * b;
  if (a % b == 0LL) {
    ans -= b;
    return;
  }
  ans += 2LL * (a % b);
  solve(a % b, b - a % b);
}
int main() {
  scanf("%lld%lld", &n, &x);
  ans = n;
  solve(x, n - x);
  printf("%lld\n", ans);
  return 0;
}
