King's Heir
InterviewTime limit2sMemory limit256 MB
Given the king's death date and each son's birth date, find the youngest son whose 18th birthday is on or before the death date, or print -1.
- Level
Easy3 of 10
- Topics
- Implementation, Math
- Solved
- No attempts yet
Problem
King Fert XIII died suddenly. The people of the Flatland Kingdom are waiting for a new king, but there is a problem. Fert had far too many sons.
Fert had sons, and he loved every newborn son more than all of his earlier sons. Each time a son was born, he wrote a new testament naming the newborn as his heir.
Only a son who is at least 18 years old at the moment of the king's death can become the new king. The ministers are trying to find the correct new king and keep getting it wrong. Help them.
Input
The first line contains three integers , and , the day, the month and the year of the king's death, with , and . Day is guaranteed to exist in month . Every month in Flatland has the same number of days as in our calendar, except that the Flatland calendar has no leap years, so February always has 28 days.
The second line contains the number of the king's sons (). The -th of the following lines contains three integers , and , the birth date of the -th son. All dates are correct, and no son was born on or after the day of the king's death. The king had no twins, so no two sons share a birth date.
Output
Print one integer, the number of the son who becomes the king, or if no son is at least 18 years old.
The sons are numbered from 1 to in the order they appear in the input. The youngest son who is at least 18 years old at the moment of the king's death becomes the king. A son whose 18th birthday falls exactly on the day of the king's death can become the king.