#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#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 <fstream>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toStr(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 FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,(n)-1)
const double EPS = 1e-12;
const double PI = acos(-1.0);
const int INF = INT_MAX/10;

struct state {
	int p, b, c;
	state(int p, int b, int c) : p(p), b(b), c(c) {};
	bool operator<(const state &o)const {
		return o.c<c;
	}
};

int main() {
	int n, m;
	while(cin >> n >> m, n) {
		vvi dist(n, vi(n, INF));
		vvi data(m, vi(3));
		REP(i, m) {
			int a, b, m;
			cin >> a >> b >> m;
			a--; b--;
			data[i][0] = m;
			data[i][1] = a;
			data[i][2] = b;
			dist[a][b] = dist[b][a] = m;
		}
		sort(ALL(data));

		int ans = INF;
		REP(i, m) {
			vector<bool> used(n, false);
			vi usedCost;
			priority_queue<state> Q;
			Q.push(state(0, -1, 0));
			while(!Q.empty()) {
				state st = Q.top();
				Q.pop();
				if(used[st.p]) {
					continue;
				} else if(st.b != -1) {
					usedCost.push_back(dist[st.b][st.p]);
				}
				used[st.p] = true;
				REP(j, n) {
					if(dist[st.p][j] != INF && st.p != j) {
						Q.push(state(j, st.p, dist[st.p][j]));
					}
				}
			}

			REP(j, n) {
				if(!used[j]) {
					goto next;
				}
			}

			sort(ALL(usedCost));
			ans = min(ans, usedCost[usedCost.size()-1]-usedCost[0]);
			dist[data[i][1]][data[i][2]] = dist[data[i][2]][data[i][1]] = INF;

			next:;
		}

		if(ans == INF) {
			cout << -1 << endl;
		} else {
			cout << ans << endl;
		}
	}
}