#include <bits/stdc++.h>
int main(void) {
  int N, X;
  scanf("%d%d", &N, &X);
  if (X > N / 2) {
    X = N - X;
  }
  int sum = 0;
  int k;
  sum += X + N - X;
  N = N - X;
  while (X != 0) {
    sum += 2 * X * (N / X);
    k = N % X;
    N = X;
    X = k;
  }
  sum -= N;
  printf("%d", sum);
}
