#include using namespace std; struct Edge { int u, v; long long w; }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, M; cin >> N >> M; vector edges(M); vector deg(N); for (auto &[u, v, w] : edges) { cin >> u >> v >> w; u--, v--; deg[u]++; deg[v]++; } vector ord(N); iota(ord.begin(), ord.end(), 0); sort(ord.begin(), ord.end(), [&](int a, int b) { if (deg[a] != deg[b]) return deg[a] > deg[b]; return a < b; }); vector owner(N); for (int i = 0; i < N; ++i) { owner[ord[i]] = i % 2; } long long ans = 0; for (auto [u, v, w] : edges) { if (owner[u] != owner[v]) continue; if (owner[u] == 0) ans += w; else ans -= w; } cout << ans << '\n'; }