#include <bits/stdc++.h>

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();}
template<class T> inline T sqr(T x) {return x*x;}

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 pb push_back
#define mp make_pair
#define each(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define exist(s,e) ((s).find(e)!=(s).end())
#define range(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)  range(i,0,n)
#define clr(a,b) memset((a), (b) ,sizeof(a))
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi  = acos(-1.0);
const ll INF =1LL << 62;
const int inf =1 << 29;


class Unionfind{
	public:
		void init(int n){
			parent.resize(n+1);
			rank.resize(n+1);
			rep(i,n+1) parent[i]=i,rank[i]=0;
		}

		int find(int x){
			return (parent[x]==x)?x:parent[x]=find(parent[x]);
		}

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

			if(rank[x]<rank[y])
				parent[x]=y;
			else{
				parent[y]=x;
				if(rank[x] == rank[y])
					rank[x]++;
			}
		}

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

	private:
		vi parent;
		vi rank;
};

typedef tuple<int,int,int> edge;

int main(void){
	Unionfind uf;
	int n,m;
	while(cin >> n >> m){
		vector<edge> edges;
		if(n==0)
			break;
		rep(i,m){
			int a,b,w;
			cin >> a >> b >> w;
			edges.emplace_back(make_tuple(w,a,b));
		}
		sort(edges.begin(),edges.end());
		int ans=inf;
		int cmin=0;
		rep(i,m){
			int cur,cmax;
			tie(cur,ignore,ignore)=edges[i];
			if(cur<=cmin)
				continue;
			cmax=cmin=cur;
			uf.init(n);
			for(int j=i;j<m;++j){
				int a,b,w;
				tie(w,a,b)=edges[j];
				if(!uf.same(a,b)){
					uf.unite(a,b);
					cmax=w;
				}
			}
			bool ok=true;
			for(int i=1;i<=n;++i)
				if(!uf.same(1,i))
					ok=false;
			if(ok)
				ans=min(ans,cmax-cmin);
		}
		if(ans==inf)
			cout << -1 << endl;
		else
			cout << ans << endl;
 	}
	return 0;
}