#include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <cassert>
using namespace std;
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
const double EPS = 1e-10;
const double PI  = acos(-1.0);


const int MAX_N=100;
int par[MAX_N];
int myrank[MAX_N];

void init(int n){
	for(int i=0;i<n;i++){
		par[i]=i;
		myrank[i]=0;
	}
}

int find(int x){
	if(par[x]==x){
		return x;
	}else{
		return par[x]=find(par[x]);
	}
}

void unite(int x,int y){
	x=find(x);
	y=find(y);
	if(x==y)return;

	if(myrank[x]<myrank[y]){
		par[x]=y;
	}else{
		par[y]=x;
		if(myrank[x]==myrank[y])myrank[x]++;
	}
}

bool same(int x,int y){
	return find(x)==find(y);
}

struct edge{
	int from,to,cost;
	edge(int from,int to,int cost):from(from),to(to),cost(cost){}
	bool operator<(const edge &other)const{
		return cost<other.cost;
	}
};
int main(){
	int n,m;
	while(cin>>n>>m,n|m){
		vector<edge>edges;
		REP(i,m){
			int a,b,w;
			cin>>a>>b>>w;
			a--;b--;
			edges.push_back(edge(a,b,w));
		}
		sort(ALL(edges));
		int ans=INT_MAX;
		REP(i,edges.size()){
			init(n);
			int cost=0;
			int cnt=0;
			FOR(j,i,edges.size()){
				if(cnt==n-1)break;
				if(!same(edges[j].from,edges[j].to)){
					unite(edges[j].from,edges[j].to);
					cost=edges[j].cost-edges[i].cost;
					cnt++;
				}
			}
			if(cnt==n-1){
				ans=min(ans,cost);
			}
		}
		cout<<(ans==INT_MAX?-1:ans)<<endl;
	}
}