#include <bits/stdc++.h>
#include <list>

using namespace std;

int rez=10000001;

vector < vector< pair<int,int> > >adj;
int V;
int prethodni;
int stanje[100001];
int promena[10001];

void Graph()
{
    adj.resize(V);
}

void addEdge(int u, int v, int t)
{
    for (int i=0;i<V;i++)
    {
        stanje[i]=0;
        promena[i]=0;
    }
    pair <int, int> pom=make_pair(v,t);
    adj[u].push_back(pom);
    pom=make_pair(u,t);
    adj[v].push_back(pom);
}

void stampaj()
{
    for (int i=0;i<V;i++)
    {
        cout<<i<<"  ";
        for (int j=0;j<adj[i].size();j++)
        {
            cout<<adj[i][j].first<<" "<<adj[i][j].second<<endl;
        }
    }
}

void printAllPathsUtil(int u, int d, bool visited[],int path[], int& path_index)
{
    visited[u]=true;
    path[path_index]=u;
    path_index++;
    //cout<<u<<endl;
    if (u==d)
    {
        //cout<<promena<<" ";
        if (promena[d]<rez)
            rez=promena[d];
    }
    else
    {
        int duzina=adj[u].size();
        for (int i=0; i<duzina; i++)
        {
            int tren=adj[u][i].first;
            //cout<<stanje<<" ";
            if (!visited[tren])
            {
                if (stanje[u]==0)
                {
                    if (adj[u][i].second==1)
                    {
                        promena[tren]=promena[u]+1;
                        stanje[tren]=1;
                    }
                    else
                    {
                        stanje[tren]=0;
                        promena[tren]=promena[u];
                    }
                }
                else
                {
                    if (adj[u][i].second==0)
                    {
                        promena[tren]=promena[u]+1;
                        stanje[tren]=0;
                    }
                    else
                    {
                        stanje[tren]=1;
                        promena[tren]=promena[u];
                    }
                }
                printAllPathsUtil(tren, d, visited, path,
                                  path_index);
            }
        }
    }
    path_index--;
    visited[u] = false;
}

void printAllPaths(int s, int d)
{
    bool visited[V];
    int path[V];
    int path_index=0;
    for (int i=0;i<V;i++)
        visited[i]=false;
    printAllPathsUtil(s,d,visited,path,path_index);
}

int main()
{
    int n,m;
    cin>>n>>m;
    V=n;
    Graph();
    int a,b,c;
    for (int i;i<m;i++)
    {
        cin>>a>>b>>c;
        a--;
        b--;
        addEdge(a,b,c);
    }
    int s,d;
    cin>>s>>d;
    s--;
    d--;
    printAllPaths(s,d);
    cout<<rez<<endl;
    return 0;
}