#include <iostream>
#include <iomanip>
#include <sstream>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <functional>
#include <iterator>
#include <limits>
#include <numeric>
#include <utility>
#include <cmath>
using namespace std; using namespace placeholders;
using LL = long long;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VS = vector<string>;
using SS = stringstream;
using PII = pair<int,int>;
using VPII = vector< pair<int,int> >;
template < typename T = int > using VT = vector<T>;
template < typename T = int > using VVT = VT< VT<T> >;
template < typename T = int > using LIM = numeric_limits<T>;
template < typename T > inline T fromString( const string &s ){ T res; istringstream iss( s ); iss >> res; return res; };
template < typename T > inline string toString( const T &a ){ ostringstream oss; oss << a; return oss.str(); };
#define REP( i, m, n ) for ( int i = (int)( m ); i < (int)( n ); ++i )
#define FOR( e, c ) for ( auto &e : c )
#define ALL( c ) (c).begin(), (c).end()
#define AALL( a, t ) (t*)a, (t*)a + sizeof( a ) / sizeof( t )
#define DRANGE( c, p ) (c).begin(), (c).begin() + p, (c).end()
#define PB( n ) push_back( n )
#define MP( a, b ) make_pair( ( a ), ( b ) )
#define EXIST( c, e ) ( (c).find( e ) != (c).end() )
#define fst first
#define snd second
#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
constexpr int INF = LIM<>::max () / 2;
int main()
{
cin.tie( 0 );
ios::sync_with_stdio( false );
int n, m;
cin >> n >> m;
VVI G( n, VI( n, INF ) );
REP( i, 0, m )
{
int a, b, t;
cin >> a >> b >> t;
a--, b--;
G[a][b] = G[b][a] = t;
}
REP( i, 0, n )
{
G[i][i] = 0;
}
REP( k, 0, n )
{
REP( i, 0, n )
{
REP( j, 0, n )
{
G[i][j] = min( G[i][j], G[i][k] + G[k][j] );
}
}
}
int res = INF;
FOR( row, G )
{
res = min( res, *max_element( ALL( row ) ) );
}
cout << res << endl;
return 0;
}