//
//  main.cpp
//  Slim Span
//
//  Created by ?嘉? on 16/8/20.
//  Copyright ? 2016年 ?嘉?. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn=100+10;
const int inf=1<<30;
int n,m,f[maxn],minimum,maximum;
struct road
{
    int u;
    int v;
    int w;
}roads[maxn*maxn/2];
int cmp(road a,road b)
{
    return a.w<b.w;
}
int getf(int i)
{
    if(f[i]==i) return i;
    else return f[i]=getf(f[i]);
}
void merge(int a,int b,int w)
{
    maximum=max(maximum,w);
    minimum=min(minimum,w);
    int fa=getf(a);
    int fb=getf(b);
    if(fa==fb) return;
    f[fa]=fb;
}
void init()
{
    for(int i=1;i<=n;i++) f[i]=i;
    minimum=inf;
    maximum=-inf;
}
int check()
{
    int cnt=0;
    int father=getf(1);
    for(int i=2;i<=n;i++)
    {
        if(getf(i)!=father) cnt++;
    }
    if(cnt==0) return 1;
    else return 0;
}
int main(int argc, const char * argv[]) {
    //freopen("/Users/zhangjiatao/Documents/暑期??/input.txt","r",stdin);
    while(scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0) break;
        for(int i=1;i<=m;i++) scanf("%d%d%d",&roads[i].u,&roads[i].v,&roads[i].w);
        sort(roads+1,roads+m+1,cmp);
        int silm=inf;
        for(int l=1;l<=m;l++)
        {
            init();
            for(int r=l;r<=m;r++)
            {
                if(getf(roads[r].u)!=getf(roads[r].v))
                {
                    merge(roads[r].u,roads[r].v,roads[r].w);
                    if(check()==1)
                    {
                        if(maximum-minimum<silm) silm=maximum-minimum;
                        break;
                    }
                }
            }
        }
        if(silm==inf) printf("-1\n");
        else printf("%d\n",silm);
    }
    return 0;
}