#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 1e3+10;
const int maxm = 1e6+10;
const int INF = 1e9;
int n, m;
struct Edge{
int v, next, weight;
bool cut;
}edge[maxm];
int head[maxn];
int tot;
int low[maxn], dfn[maxn], Stack[maxn];
int Index, top;
bool instack[maxn];
bool cut[maxn];
int add_block[maxn];
int bridge;
void add_edge(int u, int v, int weight){
edge[tot].v = v;
edge[tot].next = head[u];
edge[tot].weight = weight;
edge[tot].cut = false;
head[u] = tot++;
}
int G[maxn][maxn];
bool vis[maxn][maxn];
bool is_chong[maxn][maxn];
void tarjan(int u, int fa){
int v;
low[u] = dfn[u] = ++Index;
int son = 0;
int cnt = 0;
for(int i = head[u]; ~i; i=edge[i].next){
v = edge[i].v;
if(v == fa) {
if(cnt) low[u] = dfn[v];
cnt++;
continue;
}
if(!dfn[v]){
son++;
tarjan(v, u);
if(low[v]<low[u]) low[u] = low[v];
if(low[v]>dfn[u]){
bridge++;
edge[i].cut = true;
edge[i^1].cut = true;
}
if(low[v]>=dfn[u]&&fa!=-1){
cut[u] = true;
add_block[u]++;
}
}
else if(low[u]>dfn[v]){
low[u] = dfn[v];
}
}
if(son>1&&fa == -1) cut[u] = true;
if(fa == -1) add_block[u] = son-1;
}
void init(){
memset(head, -1, sizeof(head));
memset(instack, 0, sizeof(instack));
tot = 0;
bridge = 0;
top = Index = 0;
memset(add_block, 0, sizeof(add_block));
memset(dfn, 0, sizeof(dfn));
memset(low, 0, sizeof(low));
memset(cut, 0, sizeof(cut));
memset(vis, 0, sizeof(vis));
memset(is_chong, 0, sizeof(is_chong));
for(int i=0; i<maxn; i++)
for(int j=0; j<maxn; j++) G[i][j] = -1;
}
int min1(int a, int b){
if(a == -1) return b;
else if(b == -1) return a;
else if(a>b) return b;
else return a;
}
int main()
{
while(cin>>n>>m&&(n+m)){
init();
int u, v, weight;
for(int i=0; i<m; i++){
cin>>u>>v>>weight;
add_edge(u, v, weight);
add_edge(v, u, weight);
if(vis[u][v]) {
is_chong[u][v] = true;
is_chong[v][u] = true;
}
vis[u][v] = vis[v][u] = true;
}
tarjan(1, -1);
bool flag = false;
for(int i=1; i<=n; i++){
if(dfn[i] == 0){
flag = true;
cout<<"0"<<endl;
break;
}
}
if(flag) continue;
if(bridge == 0) {
cout<<"-1"<<endl;
}
else{
int ans = INF;
for(int i=1; i<=n; i++){
for(int j=head[i]; ~j; j = edge[j].next){
int u =i;
int v = edge[j].v;
if(edge[j].cut == true&&(!is_chong[u][v])&&(!is_chong[v][u])){
G[u][v] = edge[j].weight;
G[v][u] = edge[j].weight;
}
else if((edge[j].cut == true)&&(is_chong[u][v])){
G[u][v] = -1;
G[v][u] = -1;
}
}
}
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++){
if(i == j) continue;
if(is_chong[i][j]){
G[i][j] = G[j][i] = -1;
}
}
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
if(j == i) continue;
else{
ans = min1(ans, G[i][j]);
}
}
}
if(ans == 0) ans++;
cout<<ans<<endl;
}
}
return 0;
}