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

typedef pair<int,int> pii;

int N,M;
vector<pii> E[1000010];
int unit = 0;
int mem[200010];
int added[200010];
queue<int> Q;
vector<int> edge[200010];
vector<int> line[200010];
priority_queue<pii, vector<pii> ,greater<pii> > PQ;
int D[100010];
vector<int> V[100010];
vector<int> including;
int L;
bool visited[1000010] = {0};
vector<int> C;
bool yet[1000010] = {0};
int l;

int main()
{
    scanf("%d%d",&N,&M);
    for(int i = 0; i <= N; i++)
    {
        mem[i] = added[i] = D[i] = -1;
    }
    for(int i = 0; i < M; i++)
    {
        int p,q,c;
        scanf("%d%d%d",&p,&q,&c);
        E[c].push_back(make_pair(p,q));
        if(!yet[c])
        {
            C.push_back(c);
            yet[c] = true;
        }
    }
    for(int i = 0; i < C.size(); i++)
    {
        l = C[i];

        if(!E[l].empty())
        {
            for(int j = 0; j < E[l].size(); j++)
            {
                if(added[E[l][j].first] != l)
                {
                    including.push_back(E[l][j].first);
                    added[E[l][j].first] = l;
                }
                if(added[E[l][j].second] != l)
                {
                    including.push_back(E[l][j].second);
                    added[E[l][j].second] = l;
                }
                V[E[l][j].first].push_back(E[l][j].second);
                V[E[l][j].second].push_back(E[l][j].first);
            }
            L = unit;
            for(int j = 0; j < including.size(); j++)
            {
                if(mem[including[j]] < L)
                {
                    Q.push(including[j]);
                    while(!Q.empty())
                    {
                        int now = Q.front();
                        Q.pop();
                        if(mem[now] == unit)
                        {
                            continue;
                        }
                        mem[now] = unit;
                        edge[now].push_back(unit);
                        line[unit].push_back(now);
                        for(int k = 0; k < V[now].size(); k++)
                        {
                            Q.push(V[now][k]);
                        }
                    }
                    unit++;
                }
            }
            for(int j = 0; j < including.size(); j++)
            {
                while(V[including[j]].size())
                {
                    V[including[j]].pop_back();
                }
            }
            while(including.size())
            {
                including.pop_back();
            }
        }
    }
    PQ.push(make_pair(0,1));
    while(PQ.size())
    {
        pii now = PQ.top();
        PQ.pop();
        if(D[now.second] != -1)
        {
            continue;
        }
        D[now.second] = now.first;
        for(int i = 0; i < edge[now.second].size(); i++)
        {
            if(visited[edge[now.second][i]])
            {
                continue;
            }
            visited[edge[now.second][i]] = true;
            for(int j = 0; j < line[edge[now.second][i]].size(); j++)
            {
                PQ.push(make_pair(now.first + 1,line[edge[now.second][i]][j]));
            }
        }
    }
    printf("%d\n",D[N]);
    return 0;
}


Measured memory_bytes: 64240