#include<iostream>
#include<algorithm>
#include<cstring>
#include<set>
#include<queue>
using namespace std;
const int N=1010;
int dp[N][5010];//dp[i][j]:选到第i个物品是否能取到价值j;
int month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
struct str{int day,v;
}arr[N];
bool cmp(str x,str y){return x.day<y.day;
}
int get(int m,int d){int num=0;for(int i=1;i<m;i++) num+=month[i];return num+d;
}
int main(){int n,m,k;cin>>n>>m>>k;for(int i=1;i<=n;i++){int mm,d;cin>>mm>>d;arr[i].day=get(mm,d);cin>>arr[i].v;}sort(arr+1,arr+1+n,cmp);int cnt=0;dp[0][0]=1;for(int i=1;i<=n;i++){cnt=i-1;//找到距离i不小于k天的序号;while(cnt>0&&arr[i].day-arr[cnt].day<k) cnt--;for(int j=0;j<=m;j++){dp[i][j]=max(dp[i][j],dp[i-1][j]);if(j>=arr[i].v) dp[i][j]|=dp[cnt][j-arr[i].v];}}for(int i=m;i>=1;i--){if(dp[n][i]){cout<<i<<endl;break;}}return 0;
}