Andy had cookies and Ken had wafers. Both gave half of what they had to each other. Andy sold 9 cookies and Ken sold 27 wafers. In the end, the ratios of cookies to wafers for Andy and Ken were 1 : 5 and 1 : 3 respectively. How many cookies did Andy have at first?