##PAT 人口普查1028
//人口普查
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
struct person
{
string name;
int date;
};
int comp(person a, person b)
{
return a.date < b.date;
}
int main()
{
vector<person> people;
person b;
string name;
int year, mouth, day;
int minday = 18140905;
int maxday = 20140907;
int aday;
int n;
cin >> n;
for (int i = 0;i < n;i++)
{
cin >> name;
scanf("%d/%d/%d", &year, &mouth, &day);
aday = year * 10000 + mouth * 100 + day;
if (aday > minday&&aday < maxday)
{
b.name = name;
b.date = aday;
people.push_back(b);
}
}
sort(people.begin(), people.end(), comp);
if (people.size() == 0)
{
cout << "0" << endl;
}
else
{
cout << people.size() << " " << people[0].name << " " << people[people.size() - 1].name;
}
return 0;
}