#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <functional>
#include <algorithm>

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int*)a, (int*)(a + LENGTHOF(a)), b)
#define SQ(x) ((x)*(x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1<<29)
#define EPS 1e-10
#define MOD 1000000007

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

struct Edge{
  int u,v,cost;
  Edge(int a=0,int b=0,int c=0){ u=a;v=b;cost=c; }
  bool operator<(const Edge& a)const{ return cost < a.cost; }
};

struct UnionFind{
  int par[128];
  int rank[128];
  int s;

  UnionFind(int n){ init(n); }
  void init(int n){
    s = n;
    for(int i=0;i<n;i++){
      par[i] = i;
      rank[i] = 0;
    }
  }
  int find(int x){ return (x==par[x]?x:par[x] = find(par[x])); }
  int same(int x,int y){ return find(x)==find(y); }
  int size(){ return s; }
  void unite(int x, int y){
    x = find(x); y = find(y);
    if(x==y) return;
    s--;
    if(rank[x] < rank[y]){
      par[x] = y;
    }else{
      par[y] = x;
      if(rank[x] == rank[y]) rank[x]++;
    }
  }
};

int N,M;
Edge es[5000];

int main(){
  while(scanf("%d%d",&N,&M) && N||M){
    rep(i,M){
      int a,b,w;
      scanf("%d%d%d",&a,&b,&w); a--; b--;
      es[i] = Edge(a,b,w);
    }
    
    sort(es, es + M);
    int res = INF;
    rep(i,M){
      UnionFind uf(N);
      for(int j=i;j<M;j++){
	Edge e = es[j];
	if(!uf.same(e.u, e.v))  uf.unite(e.u, e.v);
	if(uf.size() == 1) res = min(res, es[j].cost-es[i].cost);
      }
    }

    printf("%d\n", res==INF?-1:res);
  }
  return 0;
}