#include <bits/stdc++.h>
using namespace std;
struct Wupin {
long long sl, price, cupcity, tot;
string name;
};
int main() {
long long m, n, k = 0, t, total = 0;
cin >> m >> n;
m = 21 - m;
Wupin wp1[n], wp2[10*n];
for(int i = 0; i < n; ++i) {
cin >> wp1[i].sl >> wp1[i].price >> wp1[i].cupcity >> wp1[i].name;
}
for(int i = 0; i < n; ++i) {
if(wp1[i].name != "") {
for(int j = i+1; j < n; ++j) {
if(wp1[i].name == wp1[j].name) {
wp1[i].sl += wp1[j].sl;
wp1[j].name = "";
}
}
while(wp1[i].sl > 0) {
if(wp1[i].sl >= wp1[i].cupcity) {
t = wp1[i].cupcity;
} else {
t = wp1[i].sl;
}
wp1[i].sl -= wp1[i].cupcity;
wp2[k++] = {t, wp1[i].price, wp1[i].cupcity, wp1[i].price*t, wp1[i].name};
}
}
}
sort(wp2, wp2+k, [&](Wupin w1, Wupin w2) -> bool { return w1.tot > w2.tot; });
for(int i = 0; i < m; ++i) {
total += wp2[i].tot;
}
cout << total << endl;
return 0;
}