#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define xx first
#define yy second
#define ff(i,s,f) for(int i=s;i<f;i++)
#define fb(i,s,f) for(int i=(s)-1;i>=f;i--)
#define ffi(i,s,f) for(int i=s;i<=f;i++)
#define fbi(i,s,f) for(int i=s;i>=f;i--)
#define srt(a) sort(a.begin(),a.end());
#define srtg(a,int) sort(a.begin(),a.end(),greater<int>())
#define lb(a,x) lower_bound(a.begin(),a.end(),x)
#define ub(a,x) upper_bound(a.begin(),a.end(),x)
#define fnd(a,x) find(a.begin(),a.end(),x)
#define ios ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef string str;

const int maxn=2e5+10;

vector<pii> g[maxn];
int dist[maxn][2];

void dijkstra(int poc){
    for(auto &i:dist) i[0]=i[1]=INT_MAX;
    dist[poc][0]=0;
    priority_queue<pair<int,pii> > pq;
    pq.push({0,{poc,0}});
    while(!pq.empty()){
        auto tren=pq.top();
        pq.pop();
        tren.first=-tren.first;
        if(tren.first>dist[tren.second.first][tren.second.second]) continue;
        if(dist[tren.second.first][!tren.second.second]>tren.first+1){
            dist[tren.second.first][!tren.second.second]=tren.first+1;
            pq.push({-tren.first-1,{tren.second.first,!tren.second.second}});
        }
        for(auto i:g[tren.second.first]){
            if(dist[i.first][i.second]>tren.first+int(tren.second.second!=i.second)){
                dist[i.first][i.second]=tren.first+int(tren.second.second!=i.second);
                pq.push({-tren.first-int(tren.second.second!=i.second),{i.first,i.second}});
            }
        }
    }
}

void solve(){
    int n,m;
    cin>>n>>m;
    for(int i=0;i<m;i++){
        int a,b,t;
        cin>>a>>b>>t;
        t=abs(t);
        g[a].push_back(pii(b,t));
        g[b].push_back(pii(a,t));
    }
    int u,v;
    cin>>u>>v;
    dijkstra(u);
    if(min(dist[v][0],dist[v][1])==INT_MAX){
        cout<<-1;
    }else{
        cout<<min(dist[v][0],dist[v][1]);
    }
}

int main(){
    ios;
    int t=1;
    //cin>>t;
    while(t--) solve();
    return 0;
}