#include <bits/stdc++.h>

#define FOR(type, i, a, b) for (type i = (a); i < (b); ++i)
#define RFOR(type, i, a, b) for (type i = (b)-1;i >= (a); --i)
#define REP(type, i, n) for (type i = 0; i < (n); ++i)
#define RREP(type, i, n) for (type i = (n)-1; i >= 0; --i)
#define ALL(a) (a).begin(), (a).end()
#define MIN(a, b) (a > b ? b : a)
#define MAX(a, b) (a > b ? a : b)

constexpr auto PI = 2 * acos(0.0);
constexpr auto INF = 0x33433433;

using ll = long long;
using llu = unsigned long long;

llu solve(llu a, llu b) {
  if (a == 0) return 0;
  return a * ((b / a) * 2 - 1) + solve(b % a, a);
}

int main(void) {
  llu N, X; scanf("%llu %llu", &N, &X);

  if (X < N-X) printf("%llu\n", X + N-X + X + solve(X, N-X));
  else printf("%llu\n", X + N-X + solve(N-X, X));

  return 0;
}
