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

#define Nmax 10000
int n,m;
int stanje=0;
int suma=0;

struct cvor
{
    int kraj, santa;
};

vector <cvor> graf[Nmax];


void BFS(int mika, int laza)
{
    int dist[Nmax];
    for (int i=0; i<n; i++)
        dist[i] = INT_MAX;

    deque <int> Q;
    dist[mika] = 0;
    Q.push_back(mika);

    while (!Q.empty())
    {
        int v = Q.front();
        Q.pop_front();

        for (int i=0; i<graf[v].size(); i++)
        {
            if (dist[graf[v][i].kraj] > dist[v] + graf[v][i].santa)
            {
                dist[graf[v][i].kraj] = dist[v] + graf[v][i].santa;
                 if (!graf[v][i].santa)
                  {
                      suma+=stanje^graf[v][i].santa;
                      stanje=!stanje;
                      Q.push_front(graf[v][i].kraj);
                  }
                else
                    Q.push_back(graf[v][i].kraj);
            }
        }
    }

 /*   for (int i=0; i<n; i++)
        cout << dist[i] +suma << " ";*/

        cout << (dist[laza]<<1)  << "\n";
}

void ucitajGraf(int u, int v, int t)
{
   graf[u].push_back({v, t});
   graf[v].push_back({u, t});
}

int main()
{
   ios::sync_with_stdio(false);
   cin.tie(0);
   cout.tie(0);
   cin >>n>>m;
   int a,b,t;
   for (int i=0;i<m;i++)
   {
       cin >>a>>b>>t;
       a--; b--;
       ucitajGraf(a,b,t);
   }
   int mika, laza;
   cin >>mika>>laza;
   mika--; laza--;
    BFS(mika, laza);
    return 0;
}