3 men are buried in the sand all facing the same way with only their heads above ground. Each man has a hat placed on his head which is either red or blue, but they can't see it. The men know that their hats were selected from a bag containing 3 red hats, and 2 blue hats. The man at the back (who can see the hats of the two men in front of him) is asked what hat he is wearing. He answers: "I don't know". The middle (who can see the hat of the man in front of him) man is asked what hat he is wearing. He also replies: "I don't know". Finally, the man at the front is asked what hat he is wearing. He answers: "I am wearing a red hat".
How did he know?