This is valid through the law of syllogism. If you swap lines 1 and 2, then you'll have this argument:
If I step on a beehive, then I will get stung.
If I get stung by a bee, then it will hurt.
Therefore, if I step on a beehive, then it will hurt
------------------
So it's like connecting a chain together. Point A (stepping on the hive), leads to point B (getting stung), which leads to point C (getting hurt). We can take a shortcut to bypass point B to jump from A to C in one step. Check out the attached image for a visual of what I'm referring to.