#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#include <limits>

#define phb push_back
#define mkp make_pair
#define frs first
#define scn second
#define sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()
using namespace std;

typedef pair< int, int > Pr;
typedef pair< int, Pr > Prr;
const int kInf = numeric_limits< int >::max() / 2;

int N, M;
vector< Prr > E;

int nset[110];

void read();
void solve();

int find_set(int x) {
  if (~nset[x])
    return nset[x] = find_set(nset[x]);
  return x;
}

void union_set(int x, int y) {
  nset[find_set(y)] = find_set(x);
}

int main() {
  while (cin >> N >> M, N != 0)
    read(), solve();

  return 0;
}

void read() {
  int u, v, c;

  E.clear();
  for (int i = 0; i < M; ++i) {
    cin >> u >> v >> c;
    E.phb(mkp(c, mkp(u, v)));
  }
}

void solve() {
  int res = kInf;

  stable_sort(all(E));
  for (int ei = 0; ei < sz(E); ++ei) {
    fill(&nset[0], &nset[110], -1);

    int i = ei, j = 0, t = 0, u, v;
    while (i < sz(E) && j < N - 1) {
      u = E[i].scn.frs, v = E[i].scn.scn;
      if (find_set(u) != find_set(v))
        union_set(u, v), ++j, t = E[i].frs;
      ++i;
    }

    if (j == N - 1)
      res = min(res, t - E[ei].frs);
  }

  cout << (res < kInf ? res : -1) << "\n";
}