#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;
struct UnionFind
{
  vector< int > data;
  UnionFind(int sz)
  {
    data.assign(sz, -1);
  }
  int find(int x)
  {
    if(data[x] < 0) return(x);
    return(data[x] = find(data[x]));
  }
  bool unite(int x, int y)
  {
    x = find(x), y = find(y);
    if(x == y) return(false);
    if(data[x] > data[y]) swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return(-data[x] == data.size());
  }
};
  
struct edge
{
  int u, v, cost;
  bool operator<(const edge& e) const
  {
    return(cost < e.cost);
  }
};

int main()
{
  int N, M;
  while(scanf("%d %d", &N, &M), N) {
    vector< edge > edges;
    for(int i = 0; i < M; i++) {
      int u, v, w;
      scanf("%d %d %d", &u, &v, &w);
      --u, --v;
      edges.push_back((edge){u, v, w});
    }
    sort(edges.begin(), edges.end());
    int ret = INF;
    for(int i = 0; i <= M - N + 1; i++) {
      UnionFind uf(N);
      for(int j = i; j < M && edges[j].cost - edges[i].cost < ret; j++) {
        if(uf.unite(edges[j].u, edges[j].v)) {
          ret = min(ret, edges[j].cost - edges[i].cost);
          break;
        }
      }
    }
    if(ret == INF) printf("-1\n");
    else printf("%d\n", ret);
  }
}