#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_N 101
int par[MAX_N];
int rank[MAX_N];

typedef struct {
    int p1;
    int p2;
    int weight;
} graph;

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]++;
    }
}

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

int comp(const void* c1, const void* c2);

int main() {
    int n, m, count, slim;
    graph edge[4950];

    while (1) {
        scanf("%d %d", &n, &m);
        if (n == 0) break;

        for (int i = 0; i < m; i++) {
            scanf("%d %d %d", &edge[i].p1, &edge[i].p2, &edge[i].weight);
        }

        qsort(edge, m, sizeof(graph), comp);
        slim = -1;
        for (int i = 0; i < m; i++) {
            int w = 0;
            init(n);
            unite(edge[i].p1, edge[i].p2);
            count = 1;
            for (int j = i + 1; j < m; j++) {
                if (!same(edge[j].p1, edge[j].p2)) {
                    unite(edge[j].p1, edge[j].p2);
                    count++;
                    w = edge[j].weight - edge[i].weight;
                }
            }
            if (count == n - 1) slim = slim == -1 ? w : (slim > w ? w : slim);
        }
        printf("%d\n", slim);
    }
    return 0;
}

int comp(const void* c1, const void* c2) { return (*(graph*)c1).weight - (*(graph*)c2).weight; }