#include <bits/stdc++.h>
#define ll long long
#define el "\n"
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define __ROOT__ int main()
#define fi first
#define se second
#define M 1000000007
#define MAXN 10001
#define GIOIHAN 1000001
#define BLOCK_SIZE 425
#define MAX_NODE 1001001
#define ALPHA_SIZE 26
#define compare(v) sort((v).begin(), (v).end()); (v).erase(unique((v).begin(), (v).end()), (v).end()); // dùng để nén sort mảng compare
using namespace std;
ll n,  m, mi, ma ,ans;
ll sz[MAXN], par[MAXN]  ;
struct Edge {
    ll u, v, w ;
    bool operator < (const Edge &other) const {
       return w< other.w;
    }
} edge[MAXN];
ll find_set(ll a ) {
    if(par[a] == a) return a;
    return par[a] =find_set(par[a]);
}
void uninon_set(ll a, ll b) {
    a = find_set(a) ;
    b = find_set(b);
    if(a== b) return ;
    if(sz[a] < sz[b]) swap(a,b) ;
    sz[a] += sz[b] ;
    par[b]=  a;
}
void init() {
    cin>>n >> m ;
    for(ll i =  0 ; i < m ;  i ++ ) {
        cin>>edge[i].u >> edge[i].v >> edge[i].w;
    }
    sort(edge, edge+ m );
//     for(ll i = 0 ; i  < m ;  i ++ ){
//        cout<<edge[i].u<<" " << edge[i].v<< " "<< edge[i].w<<el ;
//     }
}
void restart() {
    for(ll i = 0  ; i  <= n ; i ++ ) par[i] = i,  sz[i] = 1;
}
ll check(ll l) {
    restart() ;
    ll cnt = 0;  mi = LONG_MAX,  ma=LONG_MIN  ;
    for(ll i = l ;  i <m ; i ++ ) {
        ll x= find_set(edge[i].u);
        ll y= find_set(edge[i].v);
        if(x != y ) {
            cnt++ ;
            uninon_set(x, y) ;
            ma= max(edge[i].w,ma);
            mi = min(edge[i].w,mi);
              if(cnt == n- 1 ) return ma-mi;
        }
    }
    return LONG_MAX ;
}
void solve() {
     ans= LONG_MAX ;
    for(ll i = 0 ; i < m ; i ++ ) {
      ans = min(check(i) , ans );
//      cout<< check(i)<<el;
    }
    if(ans == LONG_MAX) {
        cout<<"NOT FOUND" ;
    } else cout<<ans;
}

__ROOT__ {
    fast;
    init();
    solve();
}
