#54562: 不廢話 直接二分搜


11430533@stu.tshs.tp.edu.tw (一孝20周定樂)


#include <bits/stdc++.h>
using namespace std;

int N,M,A,B;
vector<vector<int>> g;
vector<int> visited;

bool check(int d){
    visited.assign(N,0);
    queue<int> q;
    visited[A] = true;
    q.push(A);
    
    while (!q.empty()){
        int u = q.front();q.pop();
        for (int v=0 ; v<N ; v++){
            if (visited[v] || g[u][v] > d) continue;
            if (v == B) return true;
            visited[v] = true;
            q.push(v);
        }
    }
    return false;
}

int main(void){
    ios::sync_with_stdio(0),cin.tie(0);
    
    int l = 50005,r = 0;
    cin>>N>>M;
    g.assign(N,vector<int>(N,50005));
    while (M--){
        int u,v,w;
        cin>>u>>v>>w;
        g[u][v] = w;
        g[v][u] = w;
        if (g[u][v] < l) l = g[u][v];
        if (g[u][v] > r) r = g[u][v];
    }
    cin>>A>>B;
    int MAX = r;
    while (l <= r){
        int mid = (l+r)/2;
        if (check(mid)) r = mid-1;
        else l = mid+1;
    }
    if (l<0 || l>MAX) cout<<"-1";
    else cout<<l;
}