#include <iostream>
#include <queue>

#define ll long long
#define MAXN 200010
#define INF 100000000LL
#define pii pair<int, int>
#define xx first
#define yy second
#define pb push_back

using namespace std;

int n, m, u, v, d[MAXN][3];
vector<pii> g[MAXN];
bool vi[MAXN][3];

void dijkstra(int s)
{
    for(int i=1;i<=n;i++) d[i][0]=d[i][1]=INF;
    d[s][0]=0;
    for(int i=1;i<=n;i++) vi[i][0]=vi[i][1]=false;
    priority_queue<pair< pair<ll, int> ,int> > pq;
    pair<ll, int> p1={0, s};
    pair< pair<ll, int> ,int > p2={p1, 0};
    pq.push(p2);
    while(!pq.empty())
    {
        long long t=-pq.top().xx.xx; int u=pq.top().xx.yy; int dal=pq.top().yy; pq.pop();
        if(vi[u][dal]) continue;
        d[u][dal]=t;
        ///cout<<"u cvoru sam "<<u<<" put do njega je tezine "<<d[u][dal]<<" i posl put je bio "<<dal<<endl;
        vi[u][dal]=true;
        for(int i=0;i<g[u].size();i++) { /*cout<<g[u][i].xx<<" "<<g[u][i].yy<<endl;*/ int menjas=0; if (g[u][i].second!=dal) menjas=1; pq.push({{-t-menjas, g[u][i].xx}, g[u][i].yy}); }
    }
}

int main() {
    cin>>n>>m;

    for (int i=0; i<m; i++)
    {
        int u, v, t;
        cin>>u>>v>>t;
        g[u].pb({v, t});
        g[v].pb({u, t});
    }

    cin>>u>>v;

    dijkstra(u);

    ///cout<<d[v][0]<<" "<<d[v][1]<<endl;
    cout<<min(d[v][0], d[v][1]);
}