# POJ 3522 最大边与最小边差值最小的生成树

#include <iostream>
#include <map>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAXN 105
#define MAXM 10005
#define INF 1000000000
using namespace std;
int n, m;
int father[MAXN];
struct Edge
{
int x, y, w;
bool operator <(const Edge &a) const{
return w < a.w;
}
}edge[MAXM];
int find(int x)
{
if(father[x] == x) return x;
int t = find(father[x]);
father[x] = t;
return t;
}
int main()
{
while(scanf("%d%d", &n, &m) != EOF)
{
if(n == 0 && m == 0) break;
for(int i = 0; i < m; i++)
scanf("%d%d%d", &edge[i].x, &edge[i].y, &edge[i].w);
sort(edge, edge + m);
int ans = INF;
for(int i = 0; i <= m - n + 1; i++)
{
int tmp = -1, sum = 0;
for(int j = 1; j <= n; j++) father[j] = j;
for(int j = i; j < m; j++)
{
int fx = find(edge[j].x), fy = find(edge[j].y);
if(fx != fy)
{
sum ++;
father[fx] = fy;
if(sum == n - 1)
{
tmp = edge[j].w - edge[i].w;
break;
}
}
}
if(tmp != -1  && tmp < ans) ans = tmp;
}
if(ans < INF) printf("%d\n", ans);
else printf("-1\n");
}
return 0;
}