#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

struct Edge { int s, t, cost; };

struct UnionFind {
  vector<int> data;
  UnionFind(int n) : data(n, -1) {}
  int root(int x) { return (data[x] < 0) ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
  bool same(int x, int y) { return root(x) == root(y); }
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) return false;
    if (data[x] > data[y]) swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return true;
  }
};

int n, m;
vector<Edge> es;

int main() {
  while (cin >> n >> m, n) {
    es = vector<Edge>();

    REP(i, m) {
      int a, b, w;
      cin >> a >> b >> w, a--, b--;
      es.push_back({a, b, w});
    }

    sort(begin(es), end(es), [](Edge e1, Edge e2) { return e1.cost < e2.cost; });
    int ans = 1e9;
    REP(l, m - n + 2) {
      int mn = es[l].cost;
      int mx = 0;
      UnionFind uf(n);
      REP(r, m) {
        if (l > r) continue;
        if (uf.unite(es[r].s, es[r].t)) {
          mx = es[r].cost;
        }
        if (uf.size(0) == n) ans = min(ans, mx - mn);
      }
    }
    cout << ((ans == 1e9) ? -1 : ans) << endl;
  }
}

