the math is
 
Casquettes & chapeaux