#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std;

struct UnionFind
{
    vector<int> par;
    vector<int> sizes;

    UnionFind(int n) : par(n), sizes(n, 1) {
        for (int i = 0; i < n; ++i)
            par[i] = i;
    }

    int find(int x) {
        if (x == par[x])
            return x;
        return par[x] = find(par[x]);
    }

    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y)
            return;
        if (sizes[x] < sizes[y])
            swap(x, y);
        par[y] = x;
        sizes[x] += sizes[y];
    }

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

    int size(int x) {
        return sizes[find(x)];
    }
};

int LL;

struct Edge
{
    int a, b, cost;

    bool operator<(const Edge &o) const {
        return abs(cost - LL) < abs(o.cost - LL);
    }
};

struct Graph
{
    int n;
    vector<Edge> es;

    int kruskal() {
        sort(es.begin(), es.end());

        UnionFind uf(n);
        int min_cost = 0;

        int mi = 1000000, ma = 0;
        int t = 0;
        for (int ei = 0; ei < es.size(); ++ei) {
            Edge &e = es[ei];
            if (!uf.same(e.a, e.b)) {
                uf.unite(e.a, e.b);
                mi = min(mi, e.cost);
                ma = max(ma, e.cost);
                ++t;
            }
        }

        return (t == n - 1) ? ma - mi : -1;
    }
};

int main()
{
    for (;;) {
        int n, m;
        scanf("%d %d", &n, &m);
        if (n == 0 && m == 0)
            break;

        Graph g;
        g.n = n;
        int w[10000];
        for (int i = 0; i < m; ++i) {
            Edge e;
            scanf("%d %d %d", &e.a, &e.b, &e.cost);
            --e.a;
            --e.b;
            w[i] = e.cost;
            g.es.push_back(e);
        }

        if (m == 0) {
            printf("-1\n");
            continue;
        }

        int ans = 10000000;
        for (int i = 0; i < m; ++i) {
            LL = w[i];
            ans = min(ans, g.kruskal());
        }
        printf("%d\n", ans);
    }
    return 0;
}