// Slower C++ solution:
#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 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;

}

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

#include <cstdio>

#include <algorithm>

#include <queue>

#include <cstring>

#define N 100002

using namespace std;

struct node{

	int u,v,w;

}e[200002];

struct node1{

	int x,d;

	node1(int _x,int _d){

		x=_x,d=_d;

	}

};

vector<int> id[N];

int head[2000002],ver[2000002],nxt[2000002],edge[2000002],l;

int n,m,i,j,cnt[N],now[N],dis[2000002],num;

bool vis[2000002];

int read()

{

	char c=getchar();

	int w=0;

	while(c<'0'||c>'9') c=getchar();

	while(c<='9'&&c>='0'){

		w=w*10+c-'0';

		c=getchar();

	}

	return w;

}

int my_comp(const node &x,const node &y)

{

	return x.w<y.w;

}

void insert(int x,int y,double z)

{

	l++;

	ver[l]=y;

	edge[l]=z;

	nxt[l]=head[x];

	head[x]=l;

}

int bfs(int s)

{

	deque<node1> q;

	q.push_front(node1(s,0));

	vis[s]=1;

	while(!q.empty()){

		int x=q.front().x,d=q.front().d;

		q.pop_front();

		for(int i=head[x];i;i=nxt[i]){

			int y=ver[i];

			if(!vis[y]){

				vis[y]=1;

				if(y==num+n) return d+edge[i];

				if(edge[i]==1) q.push_back(node1(y,d+1));

				else q.push_front(node1(y,d));

			}

		}

	}

	return -1;

}

int main()

{

	n=read(),m=read();

	for(i=1;i<=m;i++) e[i].u=read(),e[i].v=read(),e[i].w=read();

	sort(e+1,e+m+1,my_comp);

	for(i=1;i<=m;i++){

		int u=e[i].u,v=e[i].v,w=e[i].w;

		if(now[u]!=w) cnt[u]++,now[u]=w,id[u].push_back(++num);

		if(now[v]!=w) cnt[v]++,now[v]=w,id[v].push_back(++num);

		insert(id[u][cnt[u]-1],id[v][cnt[v]-1],0);

		insert(id[v][cnt[v]-1],id[u][cnt[u]-1],0);

	}

	for(i=1;i<=n;i++){

		for(j=0;j<cnt[i];j++){

			insert(num+i,id[i][j],1);

			insert(id[i][j],num+i,1);

		}

	}

	int ans=bfs(num+1);

	if(ans==-1) cout<<ans<<endl;

	else cout<<ans/2<<endl;

	return 0;

}