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

if X < N-X:
  long = N - X
  short = X
else:
  long = X
  short = N - X
l += N
while True:
  l += long // short * short * 2
  x = long % short
  if x == 0:
    l -= short
    break
  long = short
  short = x
  
print(l)