#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 1e18
#define pii pair<int, int>
#define vi vector<int>
#define vl vector<int>
#define fi first
#define se second
#define lll __int128
const int N = 2005;
int n, m;
bool vis[N];
ll w[N], f[N][2];
vl g[N];
void dp(int u, int fa){
vis[u] = 1;
f[u][1] = w[u];
for(int v : g[u]){
if(v == fa)
continue;
dp(v, u);
f[u][0] += f[v][1];
f[u][1] += min(f[v][1], f[v][0]);
}
}
void solve() {
cin >> n >> m;
vector<pii> e(m + 1); //2e3
for(int i = 1; i <= m; i++){
int x, y;
cin >> x >> y >> w[i];
if(x > y)
swap(x, y);
e[i] = {x, y};
}
for(int i = 1; i <= m; i++){ //4e6
for(int j = i + 1; j <= m; j++){
int x1 = e[i].fi, y1 = e[i].se, x2 = e[j].fi, y2 = e[j].se;
bool out = 0, in = 0;
if(x2 == x1 || x2 == y1)
in = out = 1;
if(x2 > x1 && x2 < y1)
in = 1;
if(x2 > y1 || x2 < x1)
out = 1;
if(y2 == x1 || y2 == y1)
in = out = 1;
if(y2 > x1 && y2 < y1)
in = 1;
if(y2 > y1 || y2 < x1)
out = 1;
if(out && in){
g[i].push_back(j);
g[j].push_back(i);
}
}
}
ll ans = 0;
for(int i = 1; i <= m; i++){
if(g[i].size() && !vis[i]){
dp(i, 0);
ans += min(f[i][0], f[i][1]);
}
}
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}