#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
struct Man{
string name;
string birth;
int year;
int moonth;
int day;
};
//bool cmp(Man m1,Man m2){
// if(m1.year<m2.year){
// return m1.year<m2.year;
// }
// if(m1.year==m2.year&&m1.moonth<m2.moonth){
// return m1.moonth<m2.moonth;
// }
// if(m1.year==m2.year&&m1.moonth==m2.moonth&&m1.day<m2.day){
// return m1.day<m2.day;
// }
//}
int main()
{
int n;
cin>>n;
Man man[n];
Man manTemp;
for(int i=0;i<n;i++){
cin>>man[i].name>>man[i].birth;
}
for(int i=0;i<n;i++){
man[i].year=0;
man[i].moonth=0;
man[i].day=0;
}
for(int i=0;i<n;i++){
man[i].year+=(man[i].birth[0]-'0')*1000;
man[i].year+=(man[i].birth[1]-'0')*100;
man[i].year+=(man[i].birth[2]-'0')*10;
man[i].year+=man[i].birth[3]-'0';
man[i].moonth+=(man[i].birth[5]-'0')*10;
man[i].moonth+=man[i].birth[6]-'0';
man[i].day+=(man[i].birth[8]-'0')*10;
man[i].day+=man[i].birth[9]-'0';
}
for(int i=n-1;i>0;i--){
for(int j=0;j<i;j++){
if((man[j].year>man[j+1].year)||(man[j].year==man[j+1].year&&man[j].moonth>man[j+1].moonth)||(man[j].year==man[j+1].year&&man[j].moonth==man[j+1].moonth&&man[j].day>man[j+1].day)){
manTemp = man[j];
man[j]= man[j+1];
man[j+1]= manTemp;
}
}
}
// for(int i=n-1;i>0;i--){
// for(int j=0;j<i;j++){
// if(man[j].year==man[j+1].year&&man[j].moonth>man[j+1].moonth){
// manTemp = man[j];
// man[j]= man[j+1];
// man[j+1]= manTemp;
// }
// }
// }
// for(int i=n-1;i>0;i--){
// for(int j=0;j<i;j++){
// if(man[j].year==man[j+1].year&&man[j].moonth==man[j+1].moonth&&man[j].day>man[j+1].day){
// manTemp = man[j];
// man[j]= man[j+1];
// man[j+1]= manTemp;
// }
// }
// }
// sort(man,man+n,cmp);
// cout<<"\n";
// for(int i=0;i<n;i++){
// cout<<man[i].name<<" "<<man[i].birth<<"\n";
// }
int f=0;
string s1,s2;
for(int i=0;i<n;i++){
if(man[i].year<1814){
f++;
}else if(man[i].year==1814&&man[i].moonth<9){
f++;
}else if(man[i].year==1814&&man[i].moonth==9&&man[i].day<6){
f++;
}else{
s1=man[i].name;
break;
}
}
for(int i=n-1;i>=0;i--){
if((man[i].year>2014)||(man[i].year==2014&&man[i].moonth>9)||(man[i].year==2014&&man[i].moonth==9&&man[i].day>6)){
f++;
}else{
s2=man[i].name;
break;
}
}
cout<<n-f;
if((n-f)!=0){
cout<<" "<<s1<<" "<<s2;
}
return 0;
}