/*#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
class my_complex
{
public:
double a = 0,b = 0;
my_complex() : a(0),b(0) {};
my_complex(double x,double y): a(x),b(y) { };
void print()
{
if(a!=0)
{
printf("%.2lf",a);
if(b!=0)
{
if(b<0)
{
printf("-");
}
else
{
printf("+");
}
}
}
if(b!=0)
{
if(b<0)
{
printf("%.2lfi",-b);
}
else
{
printf("%.2lfi",b);
}
}
if(a==0&&b==0)
{
printf("0.00");
}
}
my_complex operator+(my_complex com)
{
my_complex answer;
answer.a = com.a + a;
answer.b = com.b + b;
return answer;
}
my_complex operator-(my_complex com)
{
my_complex answer;
answer.a = com.a + a;
answer.b = com.b + b;
return answer;
}
my_complex operator*(my_complex com)
{
my_complex answer;
answer.a = com.a * a - com.b * b;
answer.b = com.b * a + com.a * b;
return answer;
}
my_complex operator*(double c)
{
my_complex answer;
answer.a *= c;
answer.b *= c;
return answer;
}
my_complex operator/(my_complex com)
{
my_complex answer(a,b);
answer = answer * my_complex(com.a,-com.b);
answer.a /= (com.a*com.a+(com.b*com.b));
answer.b /= (com.a*com.a+(com.b*com.b));
return answer;
}
my_complex operator/(double c)
{
my_complex answer;
answer.a /= c;
answer.b /= c;
return answer;
}
bool operator==(my_complex com)
{
return com.a==a&&com.b==b;
}
void operator*=(my_complex com)
{
double temp;
temp = a;
a = com.a * a - com.b * b;
b = com.b * temp + com.a * b;
return;
}
void operator*=(double c)
{
a = a*c;
b = b*c;
return;
}
void operator/=(my_complex com)
{
double temp;
temp = a;
a = a * com.a - b * -com.b;
a /= (com.a*com.a+(com.b*com.b));
b = com.a * b + (-com.b) * temp;
b /= (com.a*com.a+(com.b*com.b));
return;
}
void operator/=(double c)
{
a /= c;
b /= c;
return;
}
void operator+=(my_complex com)
{
a += com.a;
b += com.b;
return;
}
void operator+=(double c)
{
a += c;
return;
}
void operator-=(my_complex com)
{
a -= com.a;
b -= com.b;
return;
}
void operator-=(double c)
{
a -= c;
return;
}
void operator=(my_complex com)
{
a = com.a;
b = com.b;
}
void operator=(double c)
{
a=c;
b=0;
}
};
my_complex my_sqrt(double c)
{
//printf("%lf %lf\n",c,sqrt(-c));
if(c < 0)
{
return my_complex(0,sqrt(-c));
}
else
{
return my_complex(sqrt(c),0);
}
}
int main()
{
int n,i;
double a,b,c,d;
scanf("%lf%lf%lf",&a,&b,&c);
my_complex x,y;
x = -b;
x += my_sqrt(b*b-(4*a*c));
x/= 2*a;
y = -b;
y -= my_sqrt(b*b-4*a*c);
y/= 2*a;
if(x==y)
{
x.print();
}
else
{
if(x.b==0)
{
x.print();
puts("");
y.print();
}
else
{
if(x.b>y.b)
{
x.print();
puts("");
y.print();
}
else if(x.b<y.b)
{
y.print();
puts("");
x.print();
}
}
}
}
*/
#include <stdio.h>
const int amonth[13]= {12,1,2,3,4,5,6,7,8,9,10,11,12};
const int aday[13]= {31,31,28,31,30,31,30,31,31,30,31,30,31};
struct flower
{
int start_month;
int start_day;
int end_month;
int end_day;
};
int main()
{
struct flower plant[100000];
int i,j,n;
int start_day=1;
int start_month=3;
int big_month=3;
int big_day=1;
int count=0;
int m=1;
scanf("%d",&n);
for(i=0; i < n; i++)
{
scanf("%d",&plant[i].start_month);
scanf("%d",&plant[i].start_day);
scanf("%d",&plant[i].end_month);
scanf("%d",&plant[i].end_day);
}
while(m!=0)
{
m=0;
for(i=0; i<n; i++)
{
if(plant[i].start_month<start_month)
{
if(plant[i].end_month==12)
{
printf("%d",count+1);
return 0;
}
if(plant[i].end_month>big_month)
{
big_month = plant[i].end_month;
big_day = plant[i].end_day;
m++;
}
else if(plant[i].end_month==big_month)
{
if(plant[i].end_day>big_day)
{
big_month = plant[i].end_month;
big_day = plant[i].end_day;
m++;
}
}
}
else if(plant[i].start_month==start_month)
{
if(plant[i].start_day<=start_day)
{
if(plant[i].end_month==12)
{
printf("%d",count+1);
return 0;
}
if(plant[i].end_month>big_month)
{
big_month = plant[i].end_month;
big_day = plant[i].end_day;
m++;
}
else if(plant[i].end_month==big_month)
{
if(plant[i].end_day>big_day)
{
big_month = plant[i].end_month;
big_day = plant[i].end_day;
m++;
}
}
}
}
}
count++;
start_day = big_day;
start_month = big_month;
}
printf("0");
}