A baker is preparing for a large event and needs to bake a variety of pastries. He needs to bake twice as many cupcakes as cookies, and 3 more cookies than pies. If he bakes a total of 87 pastries, how many pies does he bake? Let's think step by step and output the final answer within \boxed{}.
user