Because only one person knows.
So if the last person doesn't know
Then it means that the hat the last person saw was different in color (if it is the same, then he knows that he is another color)
So in the end, the man didn't know, so the hats on the steps were different in color.
The second person can see the color of the hat in front.
He was another color, so he changed it.