#include <bits/stdc++.h>
#define pii pair<int, int>
#define piii pair<int,pii>
#define f first
#define s second
using namespace std;
vector<piii> edges;
int par[107];
void reset( int n )
{
for( int i=1; i<=n; i++ )
par[i]= i;
}
int Find( int u )
{
if( par[u]==u )
return u;
return par[u]= Find( par[u] );
}
void unite( int u, int v )
{
int paru= Find( u );
int parv= Find( v );
if( paru!=parv )
par[paru]= parv;
}
int main()
{
int n,m;
while( scanf("%d %d", &n, &m)==2 and (n+m) )
{
for( int i=0; i<m; i++ )
{
int p,q,w;
scanf("%d %d %d", &p, &q, &w);
edges.push_back( piii( w, pii( p,q ) ) );
}
sort( edges.begin(), edges.end() );
int diff= 1e9;
for( int i=0; i<m; i++ )
{
if( m-i<n-1 )
break;
reset(n);
vector<bool> vis( n+1,0 );
int cnt= 0;
int mx= -1e9, mn= 1e9, thisdiff= 0;
for( int j=i; j<m; j++ )
{
int u= edges[j].s.f;
int v= edges[j].s.s;
if( !vis[u] )
vis[u]= 1;
if( !vis[v] )
vis[v]= 1;
if( Find(u)!=Find(v) )
{
unite(u,v);
mn= min( mn, edges[j].f );
mx= max( mx, edges[j].f );
}
}
thisdiff= mx-mn;
bool f= 1;
for( int i=1; i<=n; i++ )
{
if(!vis[i])
{
f= 0;
break;
}
if( vis[i] and Find(i)==i )
cnt++;
}
if( f and cnt==1 )
diff= min( diff, thisdiff );
}
if(diff==1e9)
diff= -1;
printf("%d\n", diff);
edges.clear();
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgcGlpIHBhaXI8aW50LCBpbnQ+CiNkZWZpbmUgcGlpaSBwYWlyPGludCxwaWk+CiNkZWZpbmUgZiBmaXJzdAojZGVmaW5lIHMgc2Vjb25kCnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp2ZWN0b3I8cGlpaT4gZWRnZXM7CmludCBwYXJbMTA3XTsKCnZvaWQgcmVzZXQoIGludCBuICkKewogICAgZm9yKCBpbnQgaT0xOyBpPD1uOyBpKysgKQogICAgICAgIHBhcltpXT0gaTsKfQoKaW50IEZpbmQoIGludCB1ICkKewogICAgaWYoIHBhclt1XT09dSApCiAgICAgICAgcmV0dXJuIHU7CiAgICByZXR1cm4gcGFyW3VdPSBGaW5kKCBwYXJbdV0gKTsKfQoKdm9pZCB1bml0ZSggaW50IHUsIGludCB2ICkKewogICAgaW50IHBhcnU9IEZpbmQoIHUgKTsKICAgIGludCBwYXJ2PSBGaW5kKCB2ICk7CgogICAgaWYoIHBhcnUhPXBhcnYgKQogICAgICAgIHBhcltwYXJ1XT0gcGFydjsKfQoKaW50IG1haW4oKQp7CiAgICBpbnQgbixtOwoKICAgIHdoaWxlKCBzY2FuZigiJWQgJWQiLCAmbiwgJm0pPT0yIGFuZCAobittKSApCiAgICB7CiAgICAgICAgZm9yKCBpbnQgaT0wOyBpPG07IGkrKyApCiAgICAgICAgewogICAgICAgICAgICBpbnQgcCxxLHc7CiAgICAgICAgICAgIHNjYW5mKCIlZCAlZCAlZCIsICZwLCAmcSwgJncpOwoKICAgICAgICAgICAgZWRnZXMucHVzaF9iYWNrKCBwaWlpKCB3LCBwaWkoIHAscSApICkgKTsKICAgICAgICB9CgogICAgICAgIHNvcnQoIGVkZ2VzLmJlZ2luKCksIGVkZ2VzLmVuZCgpICk7CgogICAgICAgIGludCBkaWZmPSAxZTk7CgogICAgICAgIGZvciggaW50IGk9MDsgaTxtOyBpKysgKQogICAgICAgIHsKICAgICAgICAgICAgaWYoIG0taTxuLTEgKQogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIHJlc2V0KG4pOwogICAgICAgICAgICB2ZWN0b3I8Ym9vbD4gdmlzKCBuKzEsMCApOwogICAgICAgICAgICBpbnQgY250PSAwOwoKICAgICAgICAgICAgaW50IG14PSAtMWU5LCBtbj0gMWU5LCB0aGlzZGlmZj0gMDsKCiAgICAgICAgICAgIGZvciggaW50IGo9aTsgajxtOyBqKysgKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBpbnQgdT0gZWRnZXNbal0ucy5mOwogICAgICAgICAgICAgICAgaW50IHY9IGVkZ2VzW2pdLnMuczsKCiAgICAgICAgICAgICAgICBpZiggIXZpc1t1XSApCiAgICAgICAgICAgICAgICAgICAgdmlzW3VdPSAxOwogICAgICAgICAgICAgICAgaWYoICF2aXNbdl0gKQogICAgICAgICAgICAgICAgICAgIHZpc1t2XT0gMTsKCiAgICAgICAgICAgICAgICBpZiggRmluZCh1KSE9RmluZCh2KSApCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgdW5pdGUodSx2KTsKICAgICAgICAgICAgICAgICAgICBtbj0gbWluKCBtbiwgZWRnZXNbal0uZiApOwogICAgICAgICAgICAgICAgICAgIG14PSBtYXgoIG14LCBlZGdlc1tqXS5mICk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KCiAgICAgICAgICAgIHRoaXNkaWZmPSBteC1tbjsKICAgICAgICAgICAgYm9vbCBmPSAxOwoKICAgICAgICAgICAgZm9yKCBpbnQgaT0xOyBpPD1uOyBpKysgKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBpZighdmlzW2ldKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIGY9IDA7CiAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CgogICAgICAgICAgICAgICAgaWYoIHZpc1tpXSBhbmQgRmluZChpKT09aSApCiAgICAgICAgICAgICAgICAgICAgY250Kys7CiAgICAgICAgICAgIH0KCiAgICAgICAgICAgIGlmKCBmIGFuZCBjbnQ9PTEgKQogICAgICAgICAgICAgICAgZGlmZj0gbWluKCBkaWZmLCB0aGlzZGlmZiApOwogICAgICAgIH0KCiAgICAgICAgaWYoZGlmZj09MWU5KQogICAgICAgICAgICBkaWZmPSAtMTsKCiAgICAgICAgcHJpbnRmKCIlZFxuIiwgZGlmZik7CgogICAgICAgIGVkZ2VzLmNsZWFyKCk7CiAgICB9Cn0K