#include using namespace std; const int N = 500010, M = N << 1; const int INF = 0x3f3f3f3f; int n, m; int res; // 链式前向星 int e[M], h[N], idx, w[M], ne[M]; void add(int a, int b, int c = 0) { e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++; } // Tarjan求割边 int dfn[N], low[N], ts; void tarjan(int u, int fa) { dfn[u] = low[u] = ++ts; for (int i = h[u]; ~i; i = ne[i]) { int v = e[i]; if (v == fa) continue; if (!dfn[v]) { tarjan(v, u); low[u] = min(low[u], low[v]); if (low[v] > dfn[u]) res = min(res, w[i]); } else low[u] = min(low[u], dfn[v]); } } int main() { #ifndef ONLINE_JUDGE freopen("HDU4738.in", "r", stdin); #endif while (~scanf("%d%d", &n, &m) && n) { ts = idx = 0; memset(h, -1, sizeof h); memset(dfn, 0, sizeof dfn); memset(low, 0, sizeof low); res = INF; while (m--) { int a, b, c; scanf("%d%d%d", &a, &b, &c); add(a, b, c), add(b, a, c); } int dcc_cnt = 0; for (int i = 1; i <= n; i++) if (!dfn[i]) tarjan(i, -1), dcc_cnt++; if (dcc_cnt > 1) { // tarjan多次说明不连通,直接输出0 printf("0\n"); continue; } if (res == INF) res = -1; else if (res == 0) res = 1; printf("%d\n", res); } return 0; }