#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, b) for (Int i = a; i <= b; i++)
#define FOR(i, n) for (Int i = 0; i < n; i++)
#define foreach(it, ar) for ( typeof(ar.begin()) it = ar.begin(); it != ar.end(); it++ )
#define fill(ar, val) memset(ar, val, sizeof(ar))
#define PI 3.1415926535897932385
#define uint64 unsigned long long
#define Int long long
#define int64 long long
#define all(ar) ar.begin(), ar.end()
#define pb push_back
#define ff first
#define mp make_pair
#define ss second
#define bit(n) (1<<(n))
#define Last(i) ( (i) & (-i) )
#define sq(x) ((x) * (x))
const Int inf = 1e15 ;
Int d[ 301 ][ 301 ] ;
vector<vector< pair< int, Int > > >g ( 301 ) ;
int n ;
Int djkstra( int s , int e )
{
vector<Int> d (n, inf);
d[s] = 0;
set < pair<Int,int> > q;
q.insert (make_pair (d[s], s));
while (!q.empty()) {
int v = q.begin()->second;
q.erase (q.begin());
for (size_t j=0; j<g[v].size(); ++j) {
int to = g[v][j].first;
Int len = g[v][j].second;
if (d[v] + len < d[to]) {
q.erase (make_pair (d[to], to));
d[to] = d[v] + len;
q.insert (make_pair (d[to], to));
}
}
}
if( d[e] >= inf ) d[e] = -1 ;
return d[e] ;
}
int main ( )
{
int m , s , e ;
cin >> n >> m >> s >> e ;
--s ; --e ;
FOR( i , n )FOR( j ,n )d[i][j] = inf ; // memset( d , inf , sizeof(d)) ;
FOR( i , m )
{
int u , v , w ;
cin >> u >> v >> w ;
--u ; --v ;
d[u][v] = w ;
}
for( int i = 0 ; i < n ; i ++ )
d[i][i] = 0 ;
for( int k = 0 ; k < n ; k ++ )
for( int i = 0 ; i < n ; i ++ )
for( int j = 0 ; j < n ; j ++ )
{
if( d[i][k] + d[k][j] < d[i][j] )
d[i][j] = d[i][k] + d[k][j] ;
}
FOR( i , n )
FOR( j , n )
{
if( d[i][j] < inf && d[j][i] < inf )
{ g[i].pb( mp( j , d[i][j] + d[j][i] ) ) ;
g[j].pb( mp( i , d[i][j] + d[j][i] ) ) ;
}
}
cout << dj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iAgICAgCSBmb3IoIGludCBqID0gMCA7IGogPCBuIDsgaiArKyApCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICBpZiggIGRbaV1ba10gKyBkW2tdW2pdIDwgIGRbaV1bal0gKQogICAgICAgICAgICAgICAgIAkgICAgICBkW2ldW2pdID0gZFtpXVtrXSArIGRba11bal0gOwoKICAgICAgICAgICAgfQoKCiAgICAgRk9SKCBpICwgbiApCiAgICAgIEZPUiggaiAsIG4gKQogICAgICAgewogICAgICAgICAgICBpZiggIGRbaV1bal0gPCBpbmYgJiYgZFtqXVtpXSA8IGluZiApCiAgICAgICAgICAgIAkgeyAgZ1tpXS5wYiggbXAoIGogLCBkW2ldW2pdICsgZFtqXVtpXSApICkgOwogICAgICAgICAgICAJICAgIGdbal0ucGIoIG1wKCBpICwgZFtpXVtqXSArIGRbal1baV0gKSApIDsKICAgICAgICAgICAgICAgICB9CgoKICAgICAgIH0KCgogICAgIGNvdXQgPDwgZGprc3RyYSAoIHMgLCBlICApIDsKCiB9Cg==