#include<bits/stdc++.h>
using namespace std;
struct rbsh
{
int t,f,h;
}r[101];
int dp[150][150];
bool cmp(rbsh a,rbsh b)
{
return a.t<b.t;
}
int main()
{
int d,g;
cin>>d>>g;
for(int i=1;i<=g;i++)cin>>r[i].t>>r[i].f>>r[i].h;
sort(r+1,r+g+1,cmp);
memset(dp,-1,sizeof(dp));
dp[0][0]=10;
for(int i=1;i<=g;i++)
{
for(int j=0;j<=d;j++)
{
if(dp[i-1][j]-(r[i].t-r[i-1].t)>=0)
{
if(j+r[i].h>=d)
{
cout<<r[i].t;
return 0;
}
dp[i][j]=max(dp[i][j],dp[i-1][j]+r[i].f-(r[i].t-r[i-1].t));
dp[i][j+r[i].h]=max(dp[i][j+r[i].h],dp[i-1][j]-(r[i].t-r[i-1].t));
}
}
}
int ans=10;
for(int i=1;i<=g;i++)ans+=r[i].f;
cout<<ans;
return 0;
}