#include <iostream>
#include <algorithm>
#include <queue>
#define INF 9999999
using namespace std;


int n, m;
int G[100][100];

class A {
public:
  int cost;
  int from;
  int pos;
  A(int cost = 0, int from = 0, int pos = 0) : cost(cost), from(from), pos(pos) {}
  bool operator < (const A &a) const {
    if(cost != a.cost) return cost > a.cost;
    else if(pos != a.pos) pos < a.pos;
    return from < a.from;
  }
};

int calc(int s, int e) {
  int visited[100];
  priority_queue<A> que;
  int maxW, minW;
  int count;
  maxW = minW = G[s][e];
  count = 1;
  fill(visited, visited+n, false);
  visited[s] = true;
  que.push(A(0, s, e));
  while(!que.empty()) {
    A a = que.top();
    que.pop();
    if(visited[a.pos]) continue;
    visited[a.pos] = true;

    if(G[a.from][a.pos] > maxW) maxW = G[a.from][a.pos];
    if(G[a.from][a.pos] < minW) minW = G[a.from][a.pos];

    ++count;
    if(count == n) return maxW - minW;

    for(int i = 0; i < n; ++i) {
      if(G[a.pos][i] == INF) continue;
      if(visited[i]) continue;
      if(G[a.pos][i] > maxW) {
	que.push(A(G[a.pos][i] - minW, a.pos, i));
      } else {
	que.push(A(maxW - G[a.pos][i], a.pos, i));
      }
    }
  }
  return INF;
}

void solve() {
  int ans = INF;
  for(int i = 0; i < n; ++i) {
    for(int j = 0; j < n; ++j) {
      if(i == j) continue;
      if(G[i][j] == INF) continue;
      ans = min(ans, calc(i, j));
    }
  }
  if(ans == INF) cout << -1 << endl;
  else           cout << ans << endl;
}

int main() {
  while((cin >> n >> m) && (n || m)) {
    fill(G[0], G[100], INF);

    for(int i = 0; i < m; ++i) {
      int a, b, w;
      cin >> a >> b >> w;
      --a; --b;
      G[a][b] = min(G[a][b], w);
      G[b][a] = min(G[b][a], w);
    }
    solve();
  }
}