import std.cstream, std.algorithm, std.typecons, std.stdio;

class UnionFind {
  int []par;
  this(int n){
    par = new int[n];
    for(int i = 0; i < n; i++){
      par[i] = i;
    }
  }

  int find(int v){
    if(par[v] == v) return v;
    return par[v] = find(par[v]);
  }

  void unite(int v, int u){
    v = find(v); u = find(u);
    if(v == u) return;
    par[v] = u;
  }

  bool same(int v, int u){
    return find(v) == find(u);
  }
}

alias Tuple!(int, "cost", int, "u", int, "v") Edge;
const int INF = 999999999;

int solve(int n, Edge[] edges){
  int cnt = 0, res;
  UnionFind uf = new UnionFind(n + 1);
  foreach(e; edges){
    if(uf.same(e.u, e.v))continue;
    uf.unite(e.u, e.v);
    res = e.cost - edges[0].cost;
    cnt += 1;
  }
  if(cnt != n - 1)return INF;
  return res;
}

void main(){
  int n, m, ans;
  while(true){
    din.readf(&n, &m);
    if(n == 0)break;
    Edge[] edges = new Edge[m];
    for(int i = 0; i < m; i++){
      din.readf(&edges[i].u, &edges[i].v, &edges[i].cost);
    }
    sort(edges);
    ans = INF;
    for(int i = 0; i < m; i++){
      ans = min(ans, solve(n, edges[i..$]));
    }
    if(ans == INF) ans = -1;
    writeln(ans);
  }
}