#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) x.begin(), x.end()
#define pf push_front
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define sz(x) (int)x.size()
#define f first
#define s second
#define mod 1000000007
// const int mod = 998244353;
// OVERFLOW!!!!!!!!!!!!!!!!!!
const int maxn = 2e5 + 100;
vector<pair<int, int>> g[maxn];
vector<int> d(maxn, 1e9);
int main()
{
   ios_base::sync_with_stdio(false);
   cout.tie(nullptr);
   cin.tie(nullptr);
   int n, m;
   cin >> n >> m;
   for (int i = 0; i < m; i++)
   {
      int a, b, c;
      cin >> a >> b >> c;
      g[a].pb({b, c});
      g[b].pb({a, c});
   }
   int start, end;
   cin >> start >> end;
   // samo 0-1 BFS?
   d[start] = 0;
   deque<pair<int, int>> dq;
   //.f je najkraci put, a .s je da znam da li je upaljen ili ugasen
   dq.push_front({start, 0});
   while (!dq.empty())
   {
      auto u = dq.front().f;
      auto state = dq.front().s;
      dq.pop_front();
      for (auto e : g[u])
      {
         int v = e.f, w = e.s;
         if (state == e.s) // 0
         {
            if (d[u] < d[v])
            {
               d[v] = d[u];
               dq.push_front({v, state});
            }
         }
         else
         {
            if (d[u] + 1 < d[v])
            {
               d[v] = d[u] + 1;
               dq.push_back({v, !state});
            }
         }
      }
   }
   cout << d[end] << endl;
   return 0;
}