n, x = map(int, input().split())

ans = n

wall_side = n - x
light_length = n - (n - x)
while True:
    if wall_side % light_length == 0:
        ans += (wall_side // light_length - 1) * 2 + 1
        break
    else:
        ans += 2 * (wall_side // light_length) * light_length
        wall_side, light_length = light_length, wall_side % light_length
        

print(ans)

