// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

inline int read(){

	int x=0,f=1;char ch=getchar();

	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}

	while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}

	return x*f;

}

#define MN 100005

#define inf 200000000

int n,m,d[MN];

struct edge{

	int to,c,nex;

}e[MN*4];int hr[MN],cnt;

inline void ins(int f,int t,int c){

	e[++cnt]=(edge){t,c,hr[f]};hr[f]=cnt;

}

int q[MN*40],top;

bool inq[MN];

set<int> col[MN];

inline void sp(){

	register int i,j;

	for(i=1;i<=n;i++) d[i]=inf;

	inq[1]=1;

	for(d[q[top=i=1]=1]=0;i<=top;inq[q[i++]]=false)

	for(j=hr[q[i]];j;j=e[j].nex){

		bool t=col[q[i]].find(e[j].c)==col[q[i]].end();

		if(d[e[j].to]>d[q[i]]+t){

			if(!inq[e[j].to]) inq[e[j].to]=1,q[++top]=e[j].to;

			col[e[j].to].clear(),col[e[j].to].insert(e[j].c);

			d[e[j].to]=d[q[i]]+t;

		}

		else if(d[e[j].to]==d[q[i]]+t&&col[e[j].to].find(e[j].c)==col[e[j].to].end()){

			if(!inq[e[j].to]) inq[e[j].to]=1,q[++top]=e[j].to;

			col[e[j].to].insert(e[j].c);

		}

	}

}

int main(){

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

	register int i,a,b,c;

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

	a=read(),b=read(),c=read(),ins(a,b,c),ins(b,a,c);

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

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<iostream>

#include<cstring>

#include<queue>

#include<map>

#define RG register

using namespace std;

const int N=1e5+10, M=2e5+10;

const long long mod=1e5+7;

struct dabt{

	int next,to,dis;

}e[M*6],hsh[M*2];

struct node{

	int u,dis;

	bool operator < (const node&x)const{return x.dis<dis;}

};

int n,m,head[N+2*M],k,dist[N+2*M],st[2*M],ed[2*M],cnt,kk;

int kh,h[mod+1];

bool vis[N+2*M];

template<typename I> inline void read(I &ot){

	I ch=getchar(), x=0, f=1;

	while(ch<'0' || ch>'9'){if(ch=='-') f=-1; ch=getchar();	}

	while(ch>='0' && ch<='9'){x=x*10+ch-'0'; ch=getchar();	}

	ot=x*f;}

template<typename I, typename... U> inline void read(I &x,U&... y){read(x); read(y...);}

inline void add(int from,int to,int d){

	e[++k]=(dabt){head[from],to,d}; head[from]=k;

	e[++k]=(dabt){head[to],from,d}; head[to]=k;

}

inline void adde(int from,int to,int d){hsh[++kh]=(dabt){h[from],to,d}; h[from]=kh;}

inline int id(int a,int b){

	int ss=((long long)a*n%mod+b)%mod;

	for(int i=h[ss];i;i=hsh[i].next)

	{

		int v=hsh[i].to;

		if(st[v]==a && ed[v]==b) return hsh[i].dis;

	}

	st[++kk]=a; ed[kk]=b;

	adde(ss,kk,++cnt);

	return cnt;

}

/*

map<pair<int,int>,int> P;

int id(int u,int c) {

    if(P.find(make_pair(u,c))!=P.end())

        return P[make_pair(u,c)];

    return P[make_pair(u,c)]=++cnt;

}*/

inline void dij()

{

	priority_queue<node> pn;

	while(!pn.empty()) pn.pop();

	for(int i=1;i<=n+m*2;i++) dist[i]=2e9;

	pn.push((node){1,0}); dist[1]=0;

	while(!pn.empty())

	{

		node u=pn.top(); pn.pop();

		int x=u.u; vis[x]=1;

		for(int i=head[x];i;i=e[i].next)

		{

			int v=e[i].to;

			if(dist[v]>dist[x]+e[i].dis)

			{

				dist[v]=dist[x]+e[i].dis;

				if(!vis[v]) pn.push((node){v,dist[v]});

			}

		}

	}

}

int main()

{

//	freopen("Snuke's Subway Trip.in","r",stdin);

//	freopen(". out","w",stdout);

	read(n,m); cnt=n;

	for(int i=1,a,b,c;i<=m;i++)

	{

		read(a,b,c);

		int id1=id(a,c), id2=id(b,c);

		add(a,id1,1); add(id1,id2,0); add(id2,b,1);

	}

	dij();

	printf("%d\n",dist[n]==2e9 ? -1 : dist[n]/2);

//	for(int i=1;i<=9;i++) printf("%d\n",dist[i]);

	//fclose(stdin); fclose(stdout);

	return 0;

}

// Fastest known solution for this problem: