def trapizoid_light(side, bottom):
    if (bottom - side) % side == 0:
        return 2 * (bottom - side)
    next_side = bottom % side
    return 2 * ((bottom - side) // side * side) + side + next_side + trapizoid_light(next_side, side) 

N, X = map(int, input().split())

if N % 2 == 0 and N // 2 == X:
    print(3 * X)
else:
    if X <= N // 2:
        print(2 * X + N - X + trapizoid_light(X, N - X))
    else:
        print(2 * (N - X) + X + trapizoid_light(N - X, X))