#include <cstdio>
#include <vector>
#include <algorithm>
#include <array>
#include <set>
#include <map>
#include <queue>
#include <tuple>
#include <unordered_set>
#include <unordered_map>
#include <functional>
#include <cassert>
#define repeat(i, n) for (int i = 0; (i) < int(n); ++(i))
#define repeat_from(i, m, n) for (int i = (m); (i) < int(n); ++(i))
#define whole(x) begin(x), end(x)
using namespace std;

struct disjoint_sets {
    vector<int> data;
    disjoint_sets() = default;
    explicit disjoint_sets(size_t n) : data(n, -1) {}
    bool is_root(int i) { return data[i] < 0; }
    int find_root(int i) { return is_root(i) ? i : (data[i] = find_root(data[i])); }
    int set_size(int i) { return - data[find_root(i)]; }
    int unite_sets(int i, int j) {
        i = find_root(i); j = find_root(j);
        if (i != j) {
            if (set_size(i) < set_size(j)) swap(i,j);
            data[i] += data[j];
            data[j] = i;
        }
        return i;
    }
    bool is_same(int i, int j) { return find_root(i) == find_root(j); }
};

struct edge_t { int a, b, w; };
constexpr int inf = 1e9+7;
int main() {
    while (true) {
        // input
        int n, m; scanf("%d%d", &n, &m);
        if (n == 0 and m == 0) break;
        vector<edge_t> e(m);
        repeat (i, m) {
            scanf("%d%d%d", &e[i].a, &e[i].b, &e[i].w);
            -- e[i].a;
            -- e[i].b;
        }
        // solve
        sort(whole(e), [](edge_t const & e, edge_t const & f) { return e.w < f.w; });
        int result = inf;
        for (int l = 0; l < m; ) {
            // Kruskal's method
            int cnt = 0;
            disjoint_sets ds(n);
            repeat_from (i, l, m) {
                if (e[i].w - e[l].w >= result) break;
                if (ds.is_same(e[i].a, e[i].b)) continue;
                ds.unite_sets(e[i].a, e[i].b);
                cnt += 1;
                if (cnt == n - 1) {
                    result = e[i].w - e[l].w;
                    break;
                }
            }
            // update
            int r = l;
            while (r < m and e[r].w == e[l].w) ++ r;
            l = r;
        }
        // output
        printf("%d\n", result == inf ? -1 : result);
    }
    return 0;
}