#include <bits/stdc++.h>
using namespace std;
long long n, x, ans;
inline long long read() {
  long long X = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
  return w ? -X : X;
}
inline void dfs(register long long l, register long long s) {
  if (l % s == 0) {
    ans += l + s;
    return;
  }
  ans += (l % s + 1) * s;
  dfs(s, l % s);
}
int main() {
  n = read(), x = read();
  ans = n;
  if (x >= n - x)
    dfs(x, n - x);
  else
    dfs(n - x, x);
  printf("%lld\n", ans);
  return 0;
}
