#if 0



#endif // 0

#include<cstdlib>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<cstring>
#include<cmath>
using namespace std;
const int MAXN = (int) 1e5 + 10;
const int INF = (int) 0x3f3f3f3f;
typedef long long LL;
int M,N;
int p[MAXN];
vector<pair<int, pair<int,int> > > g;

int Find(int x){
    return p[x]==x ? x : p[x]=Find(p[x]);
}

int same(int u,int v){
    return Find(u)==Find(v);
}

void unite(int u,int v){
    p[Find(u)]=Find(v);
}

int Kruskal(int l,int r){
    int cnt=0;
    for(int i=1;i<=N;i++) p[i]=i;
    for(int i=l;i<r;i++){
        int u=g[i].second.first;
        int v=g[i].second.second;
        if(!same(u,v)){
            cnt++;
            unite(u,v);
        }
    }
    return cnt==N-1;
}

void work(){
    while(~scanf("%d%d",&N,&M)){
        if(N==0&&M==0) break;
        g.clear();
        for(int i=0;i<N;i++) p[i]=i;
        for(int i=0;i<M;i++){
            int u,v,w; scanf("%d%d%d",&u,&v,&w);
            g.push_back(make_pair(w,make_pair(u,v)));
        }
        sort(g.begin(),g.end());
        int ans=INF;
        int L=0,R=10000;
        int siz=g.size();
        while(L<=R){
            int mid=(L+R)>>1;
//            printf("L = %d , R = %d\n",L,R);
            int l=0,r=0,ok=0;
            while(!ok&&r<siz){
                while(r<siz&&g[r].first-g[l].first<=mid) r++;
                if(Kruskal(l,r)) ok=1;
                l++;
            }
            if(ok) ans=mid,R=mid-1;
            else L=mid+1;
        }
        printf("%d\n",ans == INF ? -1 : ans);
    }
}

int main(){
    work();
    return 0;
}

