#include <bits/stdc++.h>
#define ll long long
#define ii pair<ll,ll>
#define fi first
#define se second
using namespace std;
const int N=1e5+6;
const int maxn=2e3+4;
ll s,n;
vector<ll>v[N];
vector<ii>v1[N];
ll dp[N];
ll tong=0;
int main()
{
cin >>s >>n;
for(int i=1;i<=n;i++){
ll x,y,z;
cin >>x >>y >>z;
ll tong=s/y;
v1[y].push_back({x,z});
}
for(int i=1;i<=maxn-1;i++){
sort(v1[i].begin(),v1[i].end(),greater<ii>());
ll tong=s/i;
for(int j=0;j<v1[i].size();j++){
if(v[i].size()<tong){
int s1=v1[i][j].se;
while(s1>0){
v[i].push_back(v1[i][j].fi);
s1--;
if(v[i].size()>=tong) break;
}
}
}
}
for(int i=0;i<=maxn-1;i++){
sort(v[i].begin(),v[i].end(),greater<ll>());
}
for(int w=1;w<=s;w++){
for(int i=0;i<v[w].size();i++){
for(int j=s;j>=0;j--){
if(j>=w){
dp[j]=max(dp[j],dp[j-w]+v[w][i]);
}
}
}
}
for(int j=0;j<=s;j++){
tong=max(tong,dp[j]);
}
cout << tong;
return 0;
}