N, X = [int(x) for x in input().split()]


def solution(N, X):
  if N<X:
    N, X = X, N
  if not N % X:
    return (2*(N//X) -1) * X
  else:
    return 2*(N//X)*X + solution(X, N%X)
      
answer = solution(N-X, X) + N
print(answer)
    
