The first boy could have taken any 5 wrong hats out of a total of 6.

Given he didn't take the hat of the next boy, the next boy could have taken any 4 wrong hats out of 5.

Given he did take the hat of the next boy, the latter would have got the wrong hat for sure, that is 5 ways.

Can you give the problem a try now?