#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pii;
const int mod = 998244353;
const int N = 1e6 + 10;
struct node {
int place;
int d;
};
struct cmp {
bool operator()(node a, node b) {
return a.d < b.d;
}
};
int vis[10005], dis[10005];
void solve() {
priority_queue<node, vector<node>, cmp> q;
int n, m, beg, en;
cin >> n >> m >> beg >> en;
vector<vector<pii>> graph(n + 1);
fill(dis, dis + n + 1, LONG_LONG_MAX);
dis[beg] = 0;
for (int i = 0; i < m; i++) {
int xx, yy, d;
cin >> xx >> yy >> d;
graph[xx].push_back({yy, d});
graph[yy].push_back({xx, d});
}
q.push({beg, 0});
while (!q.empty()) {
int x = q.top().place;
q.pop();
if (vis[x] ) continue;
vis[x] = 1;
for (auto edge : graph[x]) {
int y = edge.first;
if (x == y)continue;
int weight = edge.second;
int k = max(dis[x], weight);
if (k < dis[y]) {
dis[y] = k;
q.push({y, dis[y]});
}
}
}
cout <<dis[en]<< endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
while (t--) {
solve();
}
}