#include <bits/stdc++.h>
long N, X;
long max(long a, long b) { return a > b ? a : b; }
long min(long a, long b) { return a < b ? a : b; }
void input() { scanf("%d%d", &N, &X); }
int main() {
  input();
  long sum(N);
  long a;
  long b;
  a = max(X, N - X);
  b = min(X, N - X);
  while (1) {
    if (a % b == 0) {
      sum += a * 2 - b;
      break;
    } else {
      sum += (a - a % b) * 2;
      long tem(a);
      a = b;
      b = tem % b;
    }
  }
  printf("%d", sum);
  return 0;
}
