def run():
  N, X = map(int, input().split())
  y = N-X
  ans = N
  while True:
    y, X = max(y, X), min(y, X)
    a = y//X
    b = y%X
    ans += a*X*2
    if b == 0:
      break
    y, X = X, b
  print(ans-X)
  
  
if __name__ == '__main__':
  run()