#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>

using namespace std;

const int MAXN = (int)1e3 + 10 ;


vector< pair<int,int> > e[MAXN] ;
int low[MAXN] , dfn[MAXN] , visit[MAXN] , step ;
vector<int> bg ;

int n , m ;

void tarjan(int u,int fa){
    bool flag = true ;
    visit[u] = 1 ;
    dfn[u] = low[u] = ++step ;
    for (int i = 0 ; i < e[u].size() ; i++){
        int v = e[u][i].first ;
        if (v == fa && flag){
            flag = false ;
            continue ;
        }
        if (!dfn[v]){
            tarjan(v,u) ;
            low[u] = min(low[u],low[v]);
            if (low[v] > dfn[u])
                bg.push_back(e[u][i].second) ;
        }else if (visit[v]==1)
            low[u] = min(low[u],dfn[v]) ;
    }
    visit[u]=2;
}

int main() {
	while (~scanf("%d%d",&n,&m)){
        if (n==0 && m==0) break ;
        for (int i = 0 ; i < n ; i++) e[i].clear() ;
        for (int i = 0 ; i < m ; i++){
            int u , v , w;
            scanf("%d%d%d",&u,&v,&w) ;
            u--; v--;
            e[u].push_back(make_pair(v,w)) ;
            e[v].push_back(make_pair(u,w)) ;
        }
        for (int i = 0 ; i < n ; i++) dfn[i] = visit[i] = 0 ; step = 0 ;
        bg.clear() ;
        int cnt = 0 ;
        for (int i = 0 ; i < n ; i++)
            if (!dfn[i]) { tarjan(i,-1) ; cnt++ ; }
        if (cnt>1){
            printf("0\n");
            continue ;
        }
        if (bg.size()==0)
            printf("-1\n");
        else {
            int mx = 0x3f3f3f3f ;
            for (int i = 0 ; i < bg.size() ; i++)
                if (bg[i] < mx) mx = bg[i] ;
            printf("%d\n",mx) ;
        }
	}
    return 0;
}