Orlando bought a number of hats at equal cost for $500. He sold all but 2 for $540 at a profit of $5 for each item. How many hats did he buy?