1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <queue> 5 #include <cstdlib> 6 using namespace std; 7 #define N 1001 8 #define M 20000001 9 #define INF 0x3f3f3f3f 10 struct node 11 { 12 int u,v,next; 13 }edge[M]; 14 int first[N],DFN[N],Belong[N],stack[N],Low[N],cnum[N]; 15 int in[N],d[N]; 16 int qu[M],qv[M],qw[M]; 17 int n,m; 18 bool vis[M]; 19 int tot,scnt,top; 20 int o[M]; 21 void CL() 22 { 23 tot = scnt = top = 0; 24 memset(first,-1,sizeof(first)); 25 memset(in,0,sizeof(in)); 26 memset(DFN,0,sizeof(DFN)); 27 memset(vis,0,sizeof(vis)); 28 } 29 void add(int u,int v,int w) 30 { 31 edge[tot].u = u; 32 edge[tot].v = v; 33 edge[tot].next = first[u]; 34 first[u] = tot ++; 35 } 36 void Tarjan(int u) 37 { 38 int v,i; 39 DFN[u] = Low[u] = ++ tot; 40 stack[top++] = u; 41 in[u] = 1; 42 for(i = first[u];i != -1;i = edge[i].next) 43 { 44 v = edge[i].v; 45 if(vis[i]) continue; 46 vis[i] = vis[i^1] = 1; 47 if(!DFN[v]) 48 { 49 Tarjan(v); 50 Low[u] = min(Low[u],Low[v]); 51 } 52 else 53 { 54 Low[u] = min(Low[u],DFN[v]); 55 } 56 } 57 if(DFN[u] == Low[u]) 58 { 59 scnt ++; 60 do 61 { 62 v = stack[--top]; 63 Belong[v] = scnt; 64 in[v] = 0; 65 cnum[scnt] ++; 66 }while(u != v); 67 } 68 } 69 int find(int x) 70 { 71 int r,t; 72 r = x; 73 while(o[x] != x) 74 x = o[x]; 75 while(r != x) 76 { 77 t = o[r]; 78 o[r] = x; 79 r = t; 80 } 81 return x; 82 } 83 void merge(int x,int y) 84 { 85 x = find(x); 86 y = find(y); 87 if(x != y) 88 o[x] = y; 89 } 90 int main() 91 { 92 int n,m,i,u,v,w,minz; 93 while(scanf("%d%d",&n,&m)!=EOF) 94 { 95 if(n == 0&&m == 0) break; 96 CL(); 97 for(i = 1;i <= n;i ++) 98 { 99 o[i] = i; 100 } 101 for(i = 0;i < m;i ++) 102 { 103 scanf("%d%d%d",&u,&v,&w); 104 qu[i] = u; 105 qv[i] = v; 106 qw[i] = w; 107 merge(u,v); 108 add(u,v,w); 109 add(v,u,w); 110 } 111 int fa = find(1); 112 for(i = 2;i <= n;i ++) 113 { 114 if(find(i) != fa) 115 break; 116 } 117 if(i != n+1) 118 { 119 printf("0\n"); 120 continue; 121 } 122 minz = 10000000; 123 Tarjan(1); 124 for(i = 0;i < m;i ++) 125 { 126 if(Belong[qu[i]] != Belong[qv[i]]) 127 { 128 minz = min(minz,qw[i]); 129 } 130 } 131 if(scnt == 1) 132 printf("-1\n"); 133 else 134 { 135 if(minz == 0) 136 printf("1\n"); 137 else 138 printf("%d\n",minz); 139 } 140 } 141 142 }