#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>

using namespace std;

struct Edge {
	int to, w;
};

typedef vector< vector<Edge> > Graph;

#define INF 1e9

bool f(Graph& G, int m, int M) {
	vector<bool> flag(G.size(), false);
	stack<int> S; S.push(0);
	int cnt = 0;
	while ( !S.empty() ) {
		int n = S.top(); S.pop();

		if (flag[n]) continue;
		flag[n] = true;
		++cnt;

		for (int i = 0; i < G[n].size(); ++i) {
			Edge& e = G[n][i];
			if (m <= e.w && e.w <= M) {
				S.push(e.to);
			}
		}
	}
	return cnt == G.size();
}

int main() {
	int N, M;
	while ( cin >> N >> M, N || M ) {
		Graph G(N);
		vector<int> v;
		for (int i = 0; i < M; ++i) {
			int a, b, w; cin >> a >> b >> w; --a, --b;
			G[a].push_back( (Edge){b, w} );
			G[b].push_back( (Edge){a, w} );
			v.push_back(w);
		}

		sort( v.begin(), v.end() );
		v.erase( unique(v.begin(), v.end()), v.end() );

		int ans = -1;
		for (int i = 0; i < v.size(); ++i) {
			int l = i-1, r = v.size()-1; // (l, r]
			while ( l < r-1 ) {
				int m = (l + r) / 2;
				if ( f(G, v[i], v[m]) ) {
					r = m;
				}
				else {
					l = m;
				}
			}

			int m = v[i], M = v[r];
			if ( (ans < 0 || M-m < ans) && f(G, m, M) ) ans = M-m;
		}
		cout << ans << endl;
	}
}