#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int,int> mp;
int inf =  1e9;
typedef pair<int,mp> mmp;

int main(){
    while(1){
	int n,m;
	cin>>n>>m;
	if(n == 0 ) break;
	vector< vector<mp> > g(n);
	vector<mmp> edge( m );
	for(int i=0;i<m;i++){
	    int a,b,w;
	    cin>>a>>b>>w;
	    a--,b--;
	    g[a].push_back( mp( w , b ) );
	    g[b].push_back( mp( w , a ) );
	    edge[i] = mmp( w , mp( a, b ) );
	}
	sort( edge.begin() , edge.end() );
	int res = 1e9;
	for(int j=0;j<m;j++){
	    vector<bool> used( n , false );
	    int mini = edge[j].first;
	    int maxc = edge[j].first;
	    int a = edge[j].second.first;
	    int b = edge[j].second.second;
	    int rest = n-2;
	    used[a] = used[b] = true;
	    priority_queue<mp , vector<mp> , greater<mp> > q;
	    for(int i=0;i<g[a].size();i++)if( mini <= g[a][i].first && !used[ g[a][i].second ] ) q.push( g[a][i] );
	    for(int i=0;i<g[b].size();i++)if( mini <= g[b][i].first && !used[ g[b][i].second ] ) q.push( g[b][i] );
	    while( !q.empty()  ){
		mp now = q.top();
		q.pop();
		if( used[ now.second ] )continue;
		used[now.second] = true;
		rest--;
		maxc = max( maxc , now.first );
		for(int i=0;i<g[now.second].size();i++){
		    if( mini > g[now.second][i].first || used[ g[now.second][i].second ] )continue;
		    q.push( g[now.second][i] );
		}
	    }
	    if( rest == 0 ) res = min( res , maxc-mini );
	}
	if( res == 1e9 ) cout<<-1<<endl;
	else cout<<res<<endl;
    }

    return 0;
}

