#include<stdio.h>
#include<stdlib.h>
#define MAX_N 101

struct TEdge{
  int a,b,w;
};

typedef struct TEdge Edge;

Edge *edge;

int par[MAX_N];
int rank[MAX_N];

//Union Find Tree?????????????????????
//????????¶?????§??????????????????????????§???
//?????¨??????????´???????????????????
void init(int n){
  int i;
  for(i=0;i<n;i++){
    par[i] = i;
    rank[i] = 0;
  }
}


//x?????????????´?????±??????????????????£??¨????????????
int find(int x){
  if(par[x]==x){
    return x;
  }else {
    return par[x] = find(par[x]);
  }
}

//x,y????±?????????????????????¨??????
void unite(int x,int y){
  x =find(x);
  y =find(y);
  if(x==y){
    return ;
  }
  if(rank[x] < rank[y]){
    par[x] = y;
  }else {
    par[y] = x;
    if(rank[x] == rank[y])rank[x]++;
  }
}

//x,y????±?????????????????????????????????????
int same(int x,int y){
  return find(x)==find(y);
}

//?????????????????????(Edge.w?????????)
void EdgeSort(int s,int e,Edge* array){
  int i,j,k;
  i=s-1;
  j=e;
  while(i<j){
    while(++i<e&&array[i].w<array[e].w);
    while(--j>=s&&array[j].w>=array[e].w);
    if(i<j){
      //??¢??°????????¨??????????????????
      //?????????operator????????????
      array[i].w+=array[j].w;
      array[j].w=array[i].w-array[j].w;
      array[i].w-=array[j].w;

      array[i].a+=array[j].a;
      array[j].a=array[i].a-array[j].a;
      array[i].a-=array[j].a;

      array[i].b+=array[j].b;
      array[j].b=array[i].b-array[j].b;
      array[i].b-=array[j].b;

    }
  }
  if(i<e){
    //?????????????????????

    array[e].w+=array[i].w;
    array[i].w=array[e].w-array[i].w;
    array[e].w-=array[i].w;

    array[e].a+=array[i].a;
    array[i].a=array[e].a-array[i].a;
    array[e].a-=array[i].a;

    array[e].b+=array[i].b;
    array[i].b=array[e].b-array[i].b;
    array[e].b-=array[i].b;

    i++;
  }
  if(s<j)EdgeSort(s,j,array);
  if(e>i)EdgeSort(i,e,array);
}

int main(){
  int n,m;
  char s[16];
  int i,j,k,a,b,w;
  long long int res;
  while(1){
    if(!fgets(s,16,stdin))break;
    sscanf(s,"%d %d",&n,&m);
    if(n==0)break;
    edge=(Edge*)malloc(sizeof(Edge)*m);
    for(i=0;i<m;i++){
      fgets(s,16,stdin);
      sscanf(s,"%d %d %d",&a,&b,&w);
      edge[i].a=a;
      edge[i].b=b;
      edge[i].w=w;
    }
    
    EdgeSort(0,m-1,edge);
    res=-1;
    if(n==1)res=0;
    else for(k=0;k<=m-n+1;k++){
      init(n+1);
      i=0;
      for(j=0;k+j<m;j++){
        if(!same(edge[k+j].a,edge[k+j].b)){
          unite(edge[k+j].a,edge[k+j].b);
          i++;
          if(i>=n-1)break;
        }
      }
      if(k+j<m){
        if(res==-1||edge[k+j].w-edge[k].w<res)res=edge[k+j].w-edge[k].w;
      }
    }
    //return 0;
    free(edge);
    printf("%lld\n",res);
  }
  return 0;
}