#include <algorithm>
#include <iostream>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <bitset>
#include <complex>
#include <unistd.h>
#include <cassert>
#include <cctype>
#include <random>
#define _USE_MATH_DEFINES
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef pair<double, ll> pdlg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef tuple<double, double, double> tddd;
typedef complex<double> xy_t;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
int iinf = intmax / 8;
ll inf = llmax / 8;
double eps = 1e-11;

struct unionfind {
    int par[110];
    int rank[110];

    void init(int n) {
        for (int i = 0; i < n; i++) {
            par[i] = i;
            rank[i] = 0;
        }
    }

    int find(int x) {
        if (par[x] == x) {
            return x;
        } else {
            return par[x] = find(par[x]);
        }
    }
    
    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]++;
            }
        }
    }

    bool same(int x, int y) {
        return find(x) == find(y);
    }

    int rankfind(int x) {
        return rank[find(x)];
    }
};

//cost,from,toの順
vector<tiii> T;
vector<tiii> Krusans;
int minnum = 0, maxnum = 0;

ll Kruskal(int n, int m) {
    ll ans = 0;
    ll num = 0;
    unionfind u;
    u.init(n);
    int size = T.size();
    Krusans.clear();
    for (int i = m; i < size; i++) {
        int cost = get<0>(T[i]);
        int from = get<1>(T[i]);
        int to = get<2>(T[i]);
        if (u.find(from) != u.find(to)) {
            u.unite(from, to);
            ans += cost;
            num++;
            if (num == 1) {
                minnum = cost;
            }
            if (num == n - 1) {
                maxnum = cost;
            }
            tiii t = make_tuple(cost, from, to);
            Krusans.push_back(t);
        }
        if (num == n - 1) {
            break;
        }
    }
    if (num == n - 1) {
        return ans;
    } else {
        return llmax;
    }
}

int main() {
    while (1) {
        int n, m;
        cin >> n >> m;
        if (n == 0) {
            break;
        }
        T.clear();
        for (int i = 0; i < m; i++) {
            int a, b, w;
            cin >> a >> b >> w;
            T.push_back(make_tuple(w, a - 1, b - 1));
        }
        sort(T.begin(), T.end());
        ll ans = llmax;
        for (int i = 0; i < m; i++) {
            //cout << "aaa" << endl;
            ll p = Kruskal(n, i);
            //cout << p << endl;
            if (p != llmax) {
                if (maxnum - minnum < ans) {
                    ans = maxnum - minnum;
                }
            }
        }
        if (ans == llmax) {
            cout << -1 << endl;
        } else {
            cout << ans << endl;
        }
    }
}



Measured memory_bytes: 3132