#include <bits/stdc++.h>
// #ifndef ONLINE_JUDGE
// #include <debug.h>
// #else
// #define debug(...)
// #endif
#define GOOD_LUCK ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
// #define int long long
#define endl "\n"
#define ff first
#define ss second
#define pb push_back
#define all(v) v.begin(), v.end()
using namespace std;
constexpr int MAX = 2e+5 + 1, INF = 2e+9, MOD = 1e+9 + 7, K = 31;
struct point{
double p;
int v, w, k;
};
bool comp(const point &a, const point &b) {
return a.p > b.p;
}
void _() {
int s, n;
cin >> s >> n;
vector <array <int, 3>> v(n);
for (auto &i : v) cin >> i[0] >> i[1] >> i[2];
vector <point> a;
for (int i = 0; i < n; i++) {
point x;
x.v = v[i][0], x.w = v[i][1], x.k = v[i][2];
x.p = 1. * v[i][0] / v[i][1];
a.pb(x);
}
sort(all(a), comp);
int res = 0;
for (point &i : a) {
cerr << i.v << ' ' << i.w << ' ' << i.k << endl;
int c = s / i.w;
c = min(c, i.k);
s -= c * i.w;
res += c * i.v;
}
cout << res;
}
signed main() {
GOOD_LUCK
int tests=1;
// cin >> tests;
for (int i=1; i <= tests; i++) {
_();
cout << endl;
}
return 0;
}