def move(mode, N, X, total):
  tmp = total
  if mode == 0:
    tmp += (N-X)
    return move(1, N-X, X, tmp)
  if mode == 1:
    tmp += X
    next = 0
    if N-X == 0:
      return tmp
    return move(next, N, N-X, tmp)

N, X = map(int, input().split(" "))
print(move(0, N, X, X))