// Slower C++ solution:
#include <bits/stdc++.h>

    #define INF 4000001

    using namespace std;

    int n,m;

    struct edge{int to,co;};

    vector<edge> g[100010];

    int dis[100010];

    set<int> disc[100010];

    int main()

    {

    	ios::sync_with_stdio(0);

    	cin.tie(0);

     

    	cin>>n>>m;

    	for(int i=0;i<m;i++)

    	{

    		int a,b,c;

    		cin>>a>>b>>c;

    		g[a].push_back({b,c});

    		g[b].push_back({a,c});

    	}

     

    	dis[1]=0;

    	for(int i=2;i<=n;i++)

    		dis[i]=INF;

    	priority_queue<pair<int,int>> q;

    	q.push({-dis[1],1});

    	while(q.size())

    	{

    		int now=q.top().second;

    		int d=-q.top().first;

    		q.pop();

    		if(dis[now]<d) continue;

    		for(edge j:g[now])

    		{

    			//cout<<j.to<<endl;

    			int nd=dis[now];

    			if(!disc[now].count(j.co)) nd++;

    			if(nd<dis[j.to])

    			{

    				dis[j.to]=nd;

    				disc[j.to].clear();

    				disc[j.to].insert(j.co);

    				q.push({-dis[j.to],j.to});

    			}

    			else if(nd==dis[j.to])

    			{

    				disc[j.to].insert(j.co);

    			}

    		}

    	}

    	if(dis[n]==INF)

    		cout<<-1<<endl;

    	else

    		cout<<dis[n]<<endl;

    }

// Faster C++ solution for the same problem (measured speedup: 1.3712326258848189x):
#include <iostream>

#include <cstdio>

#include <cstdlib>

#include <cmath>

#include <algorithm>

#include <climits>

#include <cstring>

#include <string>

#include <set>

#include <map>

#include <queue>

#include <stack>

#include <vector>

#include <list>

#define rep(i,m,n) for(i=m;i<=n;i++)

#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)

#define mod 1000000007

#define inf 0x3f3f3f3f

#define vi vector<int>

#define pb push_back

#define mp make_pair

#define fi first

#define se second

#define ll long long

#define pi acos(-1.0)

#define pii pair<int,int>

#define Lson L, mid, rt<<1

#define Rson mid+1, R, rt<<1|1

const int maxn=1e5+10;

using namespace std;

ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}

ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}

int n,m,k,t,h[maxn],tot,ans[maxn];

set<int>comp[maxn];

struct node

{

    int c,id;

    node(int _c,int _id)

    {

        c=_c,id=_id;

    }

    bool operator<(const node&p)const

    {

        return c>p.c;

    }

};

struct node1

{

    int to,nxt,com;

}e[maxn<<2];

void add(int x,int y,int z)

{

    tot++;

    e[tot].to=y;

    e[tot].com=z;

    e[tot].nxt=h[x];

    h[x]=tot;

}

priority_queue<pair<int,int> >p;

int main()

{

    int i,j;

    memset(ans,inf,sizeof ans);

    scanf("%d%d",&n,&m);

    while(m--)

    {

        int a,b,c;

        scanf("%d%d%d",&a,&b,&c);

        add(a,b,c);

        add(b,a,c);

    }

    p.push({0,1});ans[1]=0;

    while(!p.empty())

    {

        int now=p.top().se,ca=-p.top().fi;

        p.pop();

        if(ans[now]<ca)continue;

        for(i=h[now];i;i=e[i].nxt)

        {

            int to=e[i].to,to_com=e[i].com;

            int to_ca=ca+(!comp[now].count(to_com));

            if(ans[to]>to_ca)

            {

                ans[to]=to_ca;

                p.push({-to_ca,to});

                comp[to].clear();

                comp[to].insert(to_com);

            }

            else if(ans[to]==to_ca)

            {

                comp[to].insert(to_com);

            }

        }

    }

    printf("%d\n",ans[n]==inf?-1:ans[n]);

    //system("Pause");

    return 0;

}