#include<stdio.h>
#include<vector>
using namespace std;
#define lg 250005
vector<int> v[lg], vc[lg], cuoada[1005];
//int *v[lg], *vc[lg], *cuoada[1005];
int n, m, np, nf, x, y, cst, i, j, k, nod, maxcost, nr[lg], nrp[1005], d[lg];
void citire(){
int kkt = 0, j, nt;
char sir[50];
fgets(sir, 50, stdin), nt = 0;
for (j = nt; sir[j] <= '9' && sir[j] >= '0'; j ++)
kkt = kkt*10+sir[j]-'0';
x = kkt, kkt = 0;
nt = j+1;
for (j = nt; sir[j] <= '9' && sir[j] >= '0'; j ++)
kkt = kkt*10+sir[j]-'0';
y = kkt, kkt = 0;
nt = j+1;
for (j = nt; sir[j] <= '9' && sir[j] >= '0'; j ++)
kkt = kkt*10+sir[j]-'0';
cst = kkt;
}
inline int max(int a, int b){
if (a < b)
return b;
return a;
}
int main()
{
freopen("pscnv.in", "rt", stdin);
freopen("pscnv.out", "wt", stdout);
scanf("%d%d%d%d\n", &n, &m, &np, &nf);
for (i = 1; i <= m; i ++){
//scanf("%d%d%d", &x, &y, &cst);
citire();
if (x != y){
nr[x] ++;
v[x].push_back(y);
vc[x].push_back(cst);
/*
v[x] = (int*)realloc(v[x], (nr[x]+1)*sizeof(int));
vc[x] = (int*)realloc(vc[x], (nr[x]+1)*sizeof(int));
v[x][nr[x]] = y;
vc[x][nr[x]] = cst;*/
maxcost = max(maxcost, cst);
}
}
nrp[0] = 1;
cuoada[0].push_back(np);
/*
cuoada[0] = (int*)realloc(cuoada[0], (nrp[0]+1)*sizeof(int));
cuoada[0][1] = np;*/
d[np] = 0;
for (i = 0; i <= maxcost; i ++)
for (j = 0; j < nrp[i]; j ++){
x = cuoada[i][j];
if (x == nf){
printf("%d\n", i);
return 0;
}
for (k = 0; k < nr[x]; k ++){
nod = v[x][k];
cst = vc[x][k];
if (cst < i){
nrp[i] ++;
/*
cuoada[i] = (int*)realloc(cuoada[i], (nrp[i]+1)*sizeof(int));
cuoada[i][nrp[i]] = nod;
*/
if ((d[nod] == 0 && nod != np) || d[nod] > i){
d[nod] = i;
cuoada[i].push_back(nod);
}
}
else{
nrp[cst] ++;
/*
cuoada[cst] = (int*)realloc(cuoada[cst], (nrp[cst]+1)*sizeof(int));
cuoada[cst][nrp[cst]] = nod;*/
if ((d[nod] == 0 && nod != np) || d[nod] > cst){
d[nod] = cst;
cuoada[cst].push_back(nod);
}
}
}
}
return 69;
}