#include<stdio.h>
int main()
{
    long N, X;
    scanf("%ld %ld", &N, &X);
    long L = X > N - X ? X : N - X;
    long S = N - L;
    printf("%ld\n",(N + f(L, S)));
    return 0;
}
long f(long L, long S) {
    if (S == 0) return -L;
    return 2 * S * (L / S) + f(S, L % S);
}