n = int(input())
Max = '1814/09/06'
Min = '2014/09/06'
max_, min_, num = '2014/09/06', '1814/09/06', 0
max_name, min_name = '', ''
for i in range(n):
name, date = input().split()
if date >= Max and date <= Min:
num += 1
if date < max_:
max_name = name
max_ = date
if date > min_:
min_name = name
min_ = date
if num == 0:
print(0)
else:
print(num, max_name, min_name)