#include<iostream>
#include<string>
using namespace std;
int main() {
int n;
cin>>n;
string name,year;
int count=0;
string mxname="",maxyear="1814/09/06",mnname="",minyear="2014/09/06";
for(int i=0; i<n; ++i) {
cin>>name>>year;
if(year<"1814/09/06"||year>"2014/09/06")
continue;
count++;
if(year>maxyear) {
mxname=name;
maxyear=year;
}
if(year<minyear) {
mnname=name;
minyear=year;
}
}
if(count)//坑
cout<<count<<" "<<mnname<<" "<<mxname;
else cout<<0;
return 0;
}