#include <bits/stdc++.h>

using namespace std;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)

int data[100];
inline int root(int x) {
  return data[x] < 0 ? x : data[x] = root(data[x]);
}

inline void unite(int x, int y) {
  x = root(x), y = root(y);
  if(x != y) {
    if(data[y] < data[x]) swap(x, y);
    data[x] += data[y]; data[y] = x;
  }
}
  
inline bool same(int x, int y) {
  return root(x) == root(y);
}

struct edges {
  int u, v, c;
  bool operator < (const edges& e) const {
    return c < e.c;
  }
};

int const INF = 1<<29;

int main() {
  
  int N, M;
  while(cin >> N >> M && (N|M)) {
    vector<edges> es;
    rep(i, M) {
      int a, b, c;
      cin >> a >> b >> c; a--, b--;
      es.push_back((edges){a, b, c});
    }
    sort(es.begin(), es.end());
    int ret = INF;
    
    for(; (int)es.size() >= N-1;) {
      memset(data, -1, sizeof data);
      int last = -1;
      int s = 0;
      rep(i, es.size()) {
	if(!same(es[i].u, es[i].v)) {
	  unite(es[i].u, es[i].v);
	  last = i;
	  s++;
	  if(s == N-1) break;
	}
      }
      rep(i, N) REP(j, i+1, N) {
	if(!same(i, j)) { goto exit; }
      }
      ret = min(ret, es[last].c-es[0].c);
      es.erase(es.begin());
    }
    
  exit:;
    cout << (ret < INF ? ret : -1) << endl;
    
  }
  
  return 0;
}