#include <cstdio>
#include <iostream>
#include <vector>
#include <list>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <iterator>
#include <cstdlib>
#include <cstring>
#include <sstream>

using namespace std;

#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
#define EQV(a,b) (EQ((a).real(),(b).real()) && EQ((a).imag(),(b).imag()))

typedef complex<double> P;
typedef pair<P,P> Edge;
typedef long long ll;

const double PI=4*atan(1.0);

//#define RED_FILE

#ifdef RED_FILE
ifstream ifs("input.txt");
ofstream ofs("output.txt");
#define cin ifs
#define cout ofs
#endif

const int MAX_V = 1000;

int cost[MAX_V][MAX_V];
int mincost[MAX_V];
bool used[MAX_V];

struct edge{
    int cost;
    int a,b;
};

int V;
const int INF = 10000000;
int n,m;
edge edges[10000];

int prim(){
    V=n;
    int res = INF;
    // »ê¼êÌÓðbaseÉµÄprim@ðÀs
    for(int k = 0; k < m; k++){
        // baseRXg©çÔÉl¦Ä¢­
        int base=edges[k].cost;
        edge &e=edges[k];
        for(int i = 0; i < V; i++){
            mincost[i] = INF;
            used[i] = false;
        }
        mincost[e.a] = 0;
        mincost[e.b] = 0;
        used[e.a]=used[e.b]=true;
        for(int u = 0; u < V; u++){
            // costªbaseÈãÌêÌÆ±ëÅAÅ¬ÌRXgði[
            if(cost[e.a][u]>=base)
                mincost[u] = min(mincost[u],cost[e.a][u]);
            if(cost[e.b][u]>=base)
                mincost[u] = min(mincost[u],cost[e.b][u]);
        }
        int maxCost=base;
        while(1){
            int v = -1;
            // XÉ®³È¢¸_Ì¤¿X©çÌÓÌRXgªÅ¬ÉÈé_ðT·
            for(int u = 0; u < V; u++){
                if(!used[u] && (v == -1 || mincost[u] < mincost[v]))
                    v = u;
            }
            if(v==-1)
                break;
            used[v] = true;
            maxCost=max(maxCost,mincost[v]);
            //res += mincost[v];
            for(int u = 0; u < V; u++){
                if(cost[v][u]>=base)
                    mincost[u] = min(mincost[u],cost[v][u]);
            }
        }
        int diff=abs(maxCost-base);
        res=min(res,diff);
    }
	return res;
}

bool passed[101];

void checkClosed(int s){
    for(int i = 0; i < n; i++){
        if(cost[s][i]!=INF){
            if(!passed[i]){
                passed[i]=true;
                checkClosed(i);
            }
        }
    }
}

int main(){

    while(cin>>n>>m&&!(n==0&&m==0)){
        memset(passed,0,sizeof(passed));
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                cost[i][j]=INF;
            }
        }
        for(int i = 0; i < m; i++){
            int a,b,w;
            cin>>a>>b>>w;
            a--;b--;
            cost[a][b]=cost[b][a]=w;
            edges[i].a=a;
            edges[i].b=b;
            edges[i].cost=w;
        }
        passed[0]=true;
        checkClosed(0);
        bool ok=true;
        for(int i = 0; i < n; i++){
            if(!passed[i])
                ok=false;
        }
        if(!ok){
            cout<<-1<<endl;
            continue;
        }
        int res=prim();
        if(res==INF)cout<<-1<<endl;
        else cout<<res<<endl;
    }

    return 0;
}

Measured memory_bytes: 1324