#include <bits/stdc++.h>
# define endl            '\n'
# define clr(x,a)        memset(x,a,sizeof(x))
# define vi              vector<int>
# define all(a)          a.begin(), a.end()
# define deb(x)          cout<<#x<<"=="<<x<<endl;
# define pb              push_back
# define sc              scanf
# define pr              printf
# define tc              int t; cin>>t; while(t--)
# define rev(s)          reverse(s.begin(),s.end())
# define linija          cout<<"___________\n";
# define sza(x)          (int)x.size()
using namespace std;
typedef long long ll;
const int mxN=200005, mxM=200005, mod=1e9+7, LOG=30;
template<typename T> T nzd(T a, T b){if(b==0) return a;else return nzd(b, a%b);}
template<typename T> T nzs(T a, T b){return(a*(b/nzd(a,b)));}
template<typename T> T stepenuj(T e, T n){T x=1,p=e;while(n){if(n&1)x=(x*p)%mod;p=(p*p)%mod;n>>=1;}return x;}
template<typename T> inline T na2(T x){return x*x;}
int n, m, dp[mxN], u, v, a, b;

struct Node{
   int node, t;
}pom;
vector<Node> graf[mxN];
struct Element{
   int t, val, node;
   bool operator <(const Element &P)const{
      return ((P.val<val) ||
              (P.val==val && P.t<t));
   }
}pomq;
priority_queue<Element> q;
bool visited[mxN];
void dikstra(){
   pomq.node=u;
   pomq.val=0;
   pomq.t=0;
//   dp[u]=0;
   q.push(pomq);
   int trcvor, trt;
   while(q.size()>0){
      if(visited[q.top().node]){
         q.pop();
         continue;
      }
      trcvor=q.top().node;
      trt=q.top().t;
      dp[trcvor]=q.top().val;
      visited[trcvor]=1;
      q.pop();
      for(int i=0; i<graf[trcvor].size(); i++){
         if(!visited[graf[trcvor][i].node]){
            pomq.node=graf[trcvor][i].node;
            pomq.t=graf[trcvor][i].t;
            pomq.val=dp[trcvor]+(trt^pomq.t);
            q.push(pomq);
         }
      }
   }
}
int main(){
   sc("%d%d", &n, &m);
   for(int i=1; i<=n; i++) dp[i]=-1;
   for(int i=1; i<=m; i++){
      sc("%d%d%d", &a, &b, &pom.t);
      pom.node=b;
      graf[a].push_back(pom);
      pom.node=a;
      graf[b].push_back(pom);
   }
   sc("%d%d", &u, &v);
   dikstra();
   cout<<dp[v]<<endl;
	return 0;
}