#include <bits/stdc++.h>
long int castle = 0;
int w(long int a, long int b);
int main(void) {
  long int N, X;
  scanf("%ld %ld", &N, &X);
  long long int ans = N;
  long int a, b;
  a = N - X;
  b = X;
  if ((N - X) < X) {
    a = X;
    b = N - X;
  }
  long int ho = w(a, b);
  ans = ans + castle;
  ans = ans - ho;
  printf("%lld", ans);
  return 0;
}
int w(long int a, long int b) {
  long int c, d, e;
  c = b;
  d = a % b;
  e = a / b;
  castle += 2 * e * b;
  if (d == 0)
    return b;
  else
    w(c, d);
}
