I have a stupid question

How does sperm know the difference btwn being inside a vagina and not? Because when my boyfriends comes like on my ass or legs after sex its not like the sperm moves. So how does it know to swim into my cervix and find an egg when its inside???