#include <bits/stdc++.h>
inline long long read() {
  long long x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') ch = getchar();
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return x;
}
long long dfs(int a, int b) {
  long long ans;
  if (a % b == 0)
    ans = (a << 1) - b;
  else {
    ans = (a / b * b) << 1;
    ans += dfs(b, a % b);
  }
  return ans;
}
int main() {
  int n, x;
  long long ans;
  n = read();
  x = read();
  ans = n;
  ans += dfs(((x) > (n - x) ? (x) : (n - x)), ((x) < (n - x) ? (x) : (n - x)));
  printf("%lld", ans);
  return 0;
}
