#include <iostream>
#include <algorithm>
using namespace std;
#define int long long
const int N = 5e3 + 10,D[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int n,a[N],ans;
bool is_leap(int x)
{
if (x % 4 != 0) return 0;
if (x % 100 == 0)
{
if (x % 400 == 0) return 1;
return 0;
}
return 1;
}
int get(int y,int m,int d,int h,int M)
{
int sum = 0;
for(int i = 1;i < m;i ++)
{
if (i == 2)
{
if (is_leap(y)) d += 29;
else d += 28;
}
else d += D[i];
}
for(int i = 1;i < y;i ++)
{
if (is_leap(i)) d += 366;
else d += 365;
}
return M + (h - 1) * 60 + (d - 1) * 3600;
}
signed main()
{
ios::sync_with_stdio(0);
cin >> n;
for(int i = 1;i <= n;i ++) cin >> a[i];
sort(a + 1,a + n + 1);
int e,m,d,h,M,e1,m1,d1,h1,M1;
scanf("%lld-%lld-%lld-%lld:%lld",&e,&m,&d,&h,&M);
scanf("%lld-%lld-%lld-%lld:%lld",&e1,&m1,&d1,&h1,&M1);
int t = get(e1,m1,d1,h1,M1) - get(e,m,d,h,M);
for(int i = 1;i <= n;i ++)
{
if (t >= a[i]) ans ++,t -= a[i];
else break;
}
cout << ans << endl;
return 0;
}