Plan B pill

Can I take a plan b pill 4 hours after having sex? Or do I have to wait a certain amount of time to take the pill to avoid pregnancy