#include<stdio.h>
typedef struct node
{ char name[6];
int year;
int month;
int day;
}man;
int main()
{ int n;
scanf("%d",&n);
int t=0;
man a[n], b[n];
for(int i=0;i<n;i++)
{
scanf("%s %d/%d/%d",&a[i].name,&a[i].year,&a[i].month,&a[i].day);
if((a[i].year==1814&&a[i].month>=9&&a[i].day>=6)||(a[i].year==2014&&a[i].month<=9&&a[i].day<=6)||(a[i].year>1814&&a[i].year<2014))
b[t++]=a[i];
}
man max,min;
max=min=b[0];
for(int i=1;i<t;i++)
{ if(b[i].year>min.year||(b[i].year==min.year&&b[i].month>=min.month&&b[i].day>min.day))
min=b[i];
else if(b[i].year<max.year||(b[i].year==max.year&&b[i].month<=max.month&&b[i].day<max.day))
max=b[i];
}
if(t!=0)printf("%d %s %s",t,max.name,min.name);
else printf("0");
return 0;
}