//by yjz
#include<bits/stdc++.h>
using namespace std;
#define FF first
#define SS second
#define PB push_back
#define MP make_pair
#define bged(v) (v).begin(),(v).end()
#define foreach(it,s) for(__typeof((s).begin()) it=(s).begin();it!=(s).end();it++)
typedef long long ll;
const int Imx=2147483647;
const ll Lbig=2e18;
const int mod=1e9+7;
//My i/o stream
struct fastio
{
	char s[100000];
	int it,len;
	fastio(){it=len=0;}
	inline char get()
	{
		if(it<len)return s[it++];it=0;
		len=fread(s,1,100000,stdin);
		if(len==0)return EOF;else return s[it++];
	}
	bool notend()
	{
		char c=get();
		while(c==' '||c=='\n')c=get();
		if(it>0)it--;
		return c!=EOF;
	}
}_buff;
#define geti(x) x=getnum()
#define getii(x,y) geti(x),geti(y)
#define getiii(x,y,z) getii(x,y),geti(z)
#define puti(x) putnum(x),putchar(' ')
#define putii(x,y) puti(x),puti(y)
#define putiii(x,y,z) putii(x,y),puti(z)
#define putsi(x) putnum(x),putchar('\n')
#define putsii(x,y) puti(x),putsi(y)
#define putsiii(x,y,z) putii(x,y),putsi(z)
inline ll getnum()
{
	ll r=0;bool ng=0;char c;c=_buff.get();
	while(c!='-'&&(c<'0'||c>'9'))c=_buff.get();
	if(c=='-')ng=1,c=_buff.get();
	while(c>='0'&&c<='9')r=r*10+c-'0',c=_buff.get();
	return ng?-r:r;
}
template<class T> inline void putnum(T x)
{
	if(x<0)putchar('-'),x=-x;
	register short a[20]={},sz=0;
	while(x)a[sz++]=x%10,x/=10;
	if(sz==0)putchar('0');
	for(int i=sz-1;i>=0;i--)putchar('0'+a[i]);
}
inline char getreal(){char c=_buff.get();while(c<=32)c=_buff.get();return c;}
ll qpow(ll x,ll k){return k==0?1:1ll*qpow(1ll*x*x%mod,k>>1)*(k&1?x:1)%mod;}
const int hs_sz=1<<22;
struct HashTable
{
	ll key[hs_sz];
	int val[hs_sz];
	void init(){memset(key,-1,sizeof(key));}
	HashTable(){init();}
	int hash(ll x){return ((x<<2)+x)&(hs_sz-1);}
	int &get(ll v)
	{
		int p=hash(v);
		while(key[p]!=-1&&key[p]!=v)p=(p+1)&(hs_sz-1);
		if(key[p]==-1)key[p]=v,val[p]=-1;
		return val[p];
	}
	int query(ll v)
	{
		int p=hash(v);
		while(key[p]!=-1&&key[p]!=v)p=(p+1)&(hs_sz-1);
		if(key[p]==-1)return -1;
		return val[p];
	}
}rikka;

const int maxn=500111;
int Tn,n,m,tot;

//map<pair<int,int>,int> mp;
vector<pair<int,int> > con[maxn];
void adde(int x,int y,int w)
{
//	cerr<<x<<","<<y<<" "<<w<<endl;
	con[x].PB(MP(y,w));
}
int getid(int x,int w)
{
	int &id=rikka.get(1ll*x*19260817+w);
	if(id==-1)
	{
		id=++tot;
		adde(x,id,1);
		adde(id,x,0);
	}
	return id;
}

bool vis[maxn];
const int maxq=1000111;
int q[maxq],qb,qe,dis[maxn];
void bfs(int S)
{
	qb=qe=0;
	q[qe++]=S;
	memset(vis,0,sizeof(vis));
	memset(dis,-1,sizeof(dis));
	dis[S]=0;
	while(qb!=qe)
	{
		int x=q[qb++];
		if(vis[x])continue;
		vis[x]=1;
		if(qb==maxq)qb=0;
		for(int i=0;i<con[x].size();i++)
		{
			int u=con[x][i].FF;
			if(dis[u]==-1||dis[x]+con[x][i].SS<dis[u])
			{
				dis[u]=dis[x]+con[x][i].SS;
	//			cerr<<"u="<<u<<" "<<dis[u]<<" "<<con[x][i].SS<<endl;
				if(con[x][i].SS==0)
				{
					qb--;
					if(qb<0)qb=maxq-1;
					q[qb]=u;
				}
				else
				{
					q[qe]=u;
					qe++;
					if(qe>=maxq)qe=0;
				}
			}
		}
	}
//	for(int i=1;i<=tot;i++)cerr<<dis[i]<<" ";cerr<<endl;
}
int main()
{
	while(_buff.notend())
	{
		rikka.init();
		for(int i=0;i<maxn;i++)con[i].clear();
		//mp.clear();
		getii(n,m);
		tot=n;
		for(int i=1;i<=m;i++)
		{
			int x,y,w;
			getiii(x,y,w);
			int vx=getid(x,w);
			int vy=getid(y,w);
			adde(vx,vy,0);
			adde(vy,vx,0);
//			cerr<<"vx="<<vx<<" vy="<<vy<<endl;
		}
		bfs(1);
		if(dis[n]==-1)cout<<"-1"<<endl;
		else cout<<dis[n]<<endl;
	}
	return 0;
} 

Measured memory_bytes: 85632