calculate_remainder=lambda pl,pr:2*(pl//pr)*pr-pr if pl%pr==0 else 2*(pl//pr)*pr+calculate_remainder(pr,pl%pr)
n,k=(int(s) for s in input().strip().split(' '))
print(str(n+calculate_remainder(n-k,k)))