#include <cstdio>
using namespace std;
int f[60][3200]={{0}};
int main(void)
{
freopen("budget.in","r",stdin);
freopen("budget.out","w",stdout);
int i,j,mon,n,pri[60],val[60],typ[60],exnum[60]={0},expos[60][2];
mon/=10;
scanf("%d %d",&mon,&n);
mon/=10;
for (i=1;i<=n;i++)
{
scanf("%d %d %d",&pri[i],&val[i],&typ[i]);
val[i]=val[i]*pri[i];
pri[i]/=10;
if (typ[i]!=0)
expos[typ[i]][exnum[typ[i]]++]=i;
}
for (i=1;i<=n;i++)
if (typ[i]!=0)
for (j=1;j<=mon;j++)
f[i][j]=f[i-1][j];
else
for (j=1;j<=mon;j++)
{
/**/
int temp,temppri;
f[i][j]=f[i-1][j];
if (j>=pri[i])
{
temp=val[i]+f[i-1][j-pri[i]];
if (temp>f[i][j])
f[i][j]=temp;
if (exnum[i]==1||exnum[i]==2)
{
temppri=pri[i]+pri[expos[i][0]];
if (j>=temppri)
{
temp=val[i]+val[expos[i][0]]+f[i-1][j-temppri];
if (temp>f[i][j])
f[i][j]=temp;
}
if (exnum[i]==2)
{
temppri=pri[i]+pri[expos[i][1]];
if (j>=temppri)
{
temp=val[i]+val[expos[i][1]]+f[i-1][j-temppri];
if (temp>f[i][j])
f[i][j]=temp;
}
temppri=pri[i]+pri[expos[i][0]]+pri[expos[i][1]];
if (j>=temppri)
{
temp=val[i]+val[expos[i][0]]+val[expos[i][1]]+f[i-1][j-temppri];
if (temp>f[i][j])
f[i][j]=temp;
}
}
}
}
/**/
}
printf("%d\n",f[n][mon]);
fclose(stdin);
fclose(stdout);
return(0);
}