#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <vector>
#define INF 1e5
using namespace std;
struct data{
	int v,c;
	data(){}
	data(int vv,int cc){
		v=vv;
		c=cc;
	}
	bool operator <(const data& d1)const{
		return d1.c<c;
	}
};

struct edge{
	int t,c;
	edge(){}
	edge(int tt,int cc){
		t=tt;
		c=cc;
	}
};

int n,m;
vector<edge> G[101];
int a[10001],b[10001],w[10001];
int dist[101];

int main(void){
	while(1){
		scanf("%d %d",&n,&m);
		if(n+m==0)break;
		for(int i=0;i<n;i++){
			G[i].clear();
		}
		for(int i=0;i<m;i++){
			scanf("%d %d %d",&a[i],&b[i],&w[i]);
			a[i]--;
			b[i]--;
			G[a[i]].push_back(edge(b[i],w[i]));
			G[b[i]].push_back(edge(a[i],w[i]));
		}
		int res=INF;
		for(int i=0;i<m;i++){
			fill(dist,dist+n,INF);
			int now=0;
			priority_queue<data> que;
			que.push(data(a[i],0));
			que.push(data(b[i],0));
			dist[a[i]]=0;
			dist[b[i]]=0;
			while(que.size()){
				data p=que.top();
				que.pop();
				//printf("%d\n",p.c);
				if(dist[p.v]<p.c)continue;
				now=max(now,p.c);
				for(int j=0;j<G[p.v].size();j++){
					edge e=G[p.v][j];
					int ncost=e.c-w[i];
					if(ncost<0)continue;
					if(ncost<dist[e.t]){
						dist[e.t]=ncost;
						que.push(data(e.t,ncost));
					}	
				}
			}
			bool flag=true;
			for(int i=0;i<n;i++){
				if(dist[i]==INF)flag=false;
			}
			if(flag)res=min(res,now);
		}
		printf("%d\n",res!=INF?res:-1);
	}
	return 0;
}