#include #include #include #include #include #include #include #define lc(x) ns[x].l #define rc(x) ns[x].r using namespace std; typedef long long ll; typedef unsigned long long ull; struct io_s { bool negative; char ch; inline bool isdigitx(char c) { return c >= '0' && c <= '9'; } template bool rn(T& _v) { negative = false; _v = 0; while (!isdigitx(ch = getchar()) && ch != EOF) { negative = ch == '-'; }; if (ch == EOF)return false; do { _v = _v * 10 + ch - '0'; } while (isdigitx(ch = getchar())); if (negative) _v = -_v; return true; } template bool run(T& _v) { _v = 0; while (!isdigitx(ch = getchar()) && ch != EOF) {}; if (ch == EOF)return false; do { _v = _v * 10 + ch - '0'; } while (isdigitx(ch = getchar())); return true; } template inline T r() { T v = T(); rn(v); return v; } inline int ri() { return r(); } inline ll rll() { return r(); } template inline void o(T p) { static int stk[70], tp; if (p == 0) { putchar('0'); return; } if (p < 0) { p = -p; putchar('-'); } while (p) stk[++tp] = p % 10, p /= 10; while (tp) putchar(stk[tp--] + '0'); } inline void ln() { putchar('\n'); } inline void space() { putchar(' '); } template inline void oln(T p) { o(p); ln(); } } io; const int N = 3e3 + 5; int fa[N]; int v[N]; inline int find(int x) { return fa[x] == x ? x : find(fa[x]); } inline void merge(int x, int y) { fa[find(x)] = find(y); } int main() { int n, m, ans; while (io.rn(n) && io.rn(m)) { for (int i = 1; i <= n; ++i) fa[i] = i, v[i] = 0; while (m--) { int x = io.ri(), y = io.ri(), z = io.ri(); if(x==y) continue; v[x] += z; v[y] += z; merge(x, y); } ans = v[1]; for (int i = 2; i <= n; ++i) { if (v[i] < ans)ans = v[i]; if (find(i) != find(i - 1)) { ans = 0; break; } } io.oln(ans); } return 0; }