Tom had cookies and Riordan had scones. Both gave half of what they had to each other. Tom sold 15 cookies and Riordan sold 39 scones. In the end, the ratios of cookies to scones for Tom and Riordan were 1 : 7 and 1 : 5 respectively. How many cookies did Tom have at first?