// AOJ 1280: Slim Span
// 2017.10.22 bal4u@uu

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 100
/* UNION-FIND library */
int p[MAX], rank[MAX], pp[MAX];
void make_set(int x) { p[x] = x, rank[x] = 0; }
void link(int x, int y) {
	if (rank[x] > rank[y]) p[y] = x;
	else { p[x] = y; if (rank[x] == rank[y]) rank[y] = rank[y] + 1;	}
}
int find_set(int x) { if (x != p[x]) p[x] = find_set(p[x]);	return p[x]; }
void union_set(int x, int y) { link(find_set(x), find_set(y)); }

typedef struct { int x, y, w; } EDGE;
EDGE edge[5000], *end;

int cmp(EDGE *a, EDGE *b) { return a->w - b->w; }
int main() 
{
	int n, m, i, j, k, ans, sz;
	EDGE *ep;

	for (j = 0; j < 100; j++)  pp[j] = j;
	while (scanf("%d%d", &n, &m) && n > 0) {
		sz = sizeof(int)*n;
		for (ep = edge, k = m; k--; ep++)
			scanf("%d%d%d", &i, &j, &ep->w), ep->x = i-1, ep->y = j-1;
		end = ep;

		qsort(edge, m, sizeof(EDGE), cmp);
	
		ans = -1;
		for (i = 0; i < m; i++) {
			memcpy(p, pp, sz), memset(rank, 0, sz);
			for (k = 1, ep = edge+i; ep < end; ep++) {
				if (find_set(ep->x) != find_set(ep->y)) {
					union_set(ep->x, ep->y);
					if (++k == n) {
						if (ans < 0 || ep->w - edge[i].w < ans) {
							ans = ep->w - edge[i].w;
							if (ans == 0) goto done;
						}
						break;
					}
				}
			}
		}
done:	printf("%d\n", ans);
	} 
	return 0; 
}