include <bits/stdc++.h>
using namespace std; int n, m, s = 0, a[10000][10000], d[10000];
void f() {
int p;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
p = 0;
if (a[i][j] != 0 && d[j] == 0) {
if (a[i][j + 1] != 0) {
int t;
p++;
if (a[i][j + 1] > a[i][j]) {
t = a[i][j + 1];
s += t;
} else {
t = a[i][j];
s += t;
}
d[j + 1] = 1;
} else {
s += a[i][j];
}
d[j] = 1;
}
}
}
}
int main() {
cin >> n >> m;
memset(a, 0, sizeof(a));
memset(d, 0, m);
for (int i = 1; i <= m; i++) {
int x, y, z;
cin >> x >> y >> z;
a[x][y] = z;
}
f();
cout << s;
return 0;
}