#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int N=1e2+10,INF=1000000000;
int n,m,
pa[N];
struct edge {
int u,v,d;
edge(int u,int v,int d):u(u),v(v),d(d){}
bool operator < (const edge& rhs) const{
return d<rhs.d;
}
};
vector<edge>e;
int find(int x) {return x==pa[x]?pa[x]:pa[x]=find(pa[x]);}
int solve() {
int ans=INF;
sort(e.begin(), e.end());
for(int L=0;L<m;L++) {
for(int i=1;i<=n;i++) pa[i]=i;
int cnt=n;
for(int R=L;R<m;R++) {
int u=find(e[R].u),v=find(e[R].v);
if(u != v) {
pa[u] = v;
if(--cnt == 1) { ans = min(ans, e[R].d-e[L].d); break; }
}
}
}
if(ans == INF) ans = -1;
return ans;
}
int main() {
while(scanf("%d%d",&n,&m)==2&&n) {
e.clear();
int u,v,w;
for(int i=1;i<=m;++i) {
scanf("%d%d%d",&u,&v,&w);
e.push_back(edge(u,v,w));
}
printf("%d\n",solve());
}
return 0;
}
枚举L和R,对于在区间[L,R]的边,判断连通性