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

#include <string.h>

#include <algorithm>

#define ME 200000

#define MN 1400000

#define INF 0x7f7f7f7f

int n,m,pl,ql,pc;

int lst[MN+5],pnt[MN+5];

int dis[MN+5]; bool usd[MN+5];

int hd[MN+5],to[MN+5],val[MN+5],nxt[MN+5],rn=0;

struct edge{int p,q,c;}e[ME+5];

bool cmp(edge a,edge b){return a.c<b.c;}

struct DEQUE{

	int mem[MN*2+5],hd,tl;

	DEQUE(){hd=MN,tl=MN+1;}

	int front(){return mem[hd];}

	bool empty(){return hd==tl-1;}

	void hdpush(int x){mem[++hd]=x;}

	void hdpop(){--hd;}

	void tlpush(int x){mem[--tl]=x;}

	void tlpop(){++tl;}

}q;

inline void add(int u,int v,int c){

	++rn;

	nxt[rn]=hd[u],hd[u]=rn;

	to[rn]=v,val[rn]=c;

}

int main(){

	memset(dis,0x7f,sizeof(dis));

	memset(hd,0xff,sizeof(hd));

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

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

		scanf("%d%d%d",&e[i].p,&e[i].q,&e[i].c);

	std::sort(e+1,e+1+m,cmp);

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

		if(lst[e[i].p]!=e[i].c){

			lst[e[i].p]=e[i].c,pnt[e[i].p]=(++pc);

			add(e[i].p,pnt[e[i].p],1),add(pnt[e[i].p],e[i].p,0);

		}

		if(lst[e[i].q]!=e[i].c){

			lst[e[i].q]=e[i].c,pnt[e[i].q]=(++pc);

			add(e[i].q,pnt[e[i].q],1),add(pnt[e[i].q],e[i].q,0);

		}

		add(pnt[e[i].p],pnt[e[i].q],0),add(pnt[e[i].q],pnt[e[i].p],0);

	}

	dis[1]=0; q.tlpush(1);

	for(int d=0;;d++){

		while(1){

			int u=q.front();

			if(usd[u]){q.hdpop();continue;}

			if(dis[u]!=d)break;

			q.hdpop(); usd[u]=true;

			for(int i=hd[u];i!=-1;i=nxt[i]){

				if(!usd[to[i]]){

					if(val[i]==0){

						if(dis[to[i]]>d){

							dis[to[i]]=d;

							q.hdpush(to[i]);

						}

					}else{

						if(dis[to[i]]>d+1){

							dis[to[i]]=d+1;

							q.tlpush(to[i]);

						}

					}

				}

			}

		}

		if(dis[n]!=INF||q.empty())break;

	}

	printf("%d",dis[n]==INF?-1:dis[n]);

}

// Faster C++ solution for the same problem (measured speedup: 1.2468344215215126x):
#include<bits/stdc++.h>

using namespace std;



typedef pair<int,int> pii;

typedef pair<int,pii> edge;



const int N = 1e5;



int n,m;

edge li[2*N+5];



vector<int> isi[2*N+5];

vector<int> punya[N+5];



int find_par(int x, int *par){

	if(par[x] == x) return x;

	return par[x] = find_par(par[x],par);

}



int par[N+5];

bool vis[N+5];

map<int,int> uda;

bool hdone[2*N+5];



int bfs(int x){

	memset(vis,0,sizeof(vis));

	memset(hdone,0,sizeof(hdone));

	queue<int> qx;

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

	qx.push(1);

	int ret = 0;

	while(!qx.empty()){

		ret++;

		int pan = qx.size();

		while(pan--){

			int cur = qx.front(); qx.pop();

			if(vis[cur]) continue;

			vis[cur] = 1;

			for(int i=0;i<punya[cur].size();i++){

				int head = punya[cur][i];

				if(hdone[head]) continue;

				hdone[head] = 1;

				for(int j=0;j<isi[head].size();j++){

					int now = isi[head][j];

					if(vis[now]) continue;

					if(now == n) return ret;

					qx.push(now);

				}

			}

		}

	}

	return -1;

}



int main(){

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

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

		int p,q,c;

		scanf("%d%d%d",&p,&q,&c);

		li[i] = make_pair(c,make_pair(p,q));

	}

	sort(li+1,li+m+1);

	int cnt = 0;

	for(int i=1;i<=n;i++) par[i] = i;

	int l = 1;

	for(int i=2;i<=m;i++){

		if(li[i].first == li[i-1].first) continue;

		for(int j=l;j<i;j++){

			int pu = find_par(li[j].second.first,par);

			int pv = find_par(li[j].second.second,par);

			if(pu == pv) continue;

			par[pu] = pv;

		}

		for(int j=l;j<i;j++){

			int u = li[j].second.first;

			int v = li[j].second.second;

			int pu = find_par(u,par);

			int pv = find_par(v,par);

			if(!vis[u]){

				if(uda.find(pu) == uda.end()){

					cnt++;

					uda[pu] = cnt;

				}

				int idx = uda[pu];

				punya[u].push_back(idx);

				isi[idx].push_back(u);

			}

			

			if(!vis[v]){

				if(uda.find(pv) == uda.end()){

					cnt++;

					uda[pv] = cnt;

				}

				int idx = uda[pv];

				punya[v].push_back(idx);

				isi[idx].push_back(v);

			}

		}

		for(int j=l;j<i;j++){

			int u = li[j].second.first;

			int v = li[j].second.second;

			par[u] = u;

			par[v] = v;

			vis[u] = 0;

			vis[v] = 0;

		}

		l = i;

		uda.clear();

	}

	for(int j=l;j<=m;j++){

		int pu = find_par(li[j].second.first,par);

		int pv = find_par(li[j].second.second,par);

		if(pu == pv) continue;

		par[pu] = pv;

	}

	for(int j=l;j<=m;j++){

		int u = li[j].second.first;

		int v = li[j].second.second;

		int pu = find_par(u,par);

		int pv = find_par(v,par);

		if(!vis[u]){

			if(uda.find(pu) == uda.end()){

				cnt++;

				uda[pu] = cnt;

			}

			int idx = uda[pu];

			punya[u].push_back(idx);

			isi[idx].push_back(u);

		}

		

		if(!vis[v]){

			if(uda.find(pv) == uda.end()){

				cnt++;

				uda[pv] = cnt;

			}

			int idx = uda[pv];

			punya[v].push_back(idx);

			isi[idx].push_back(v);

		}

	}

	for(int j=l;j<=m;j++){

		int u = li[j].second.first;

		int v = li[j].second.second;

		par[u] = u;

		par[v] = v;

		vis[u] = 0;

		vis[v] = 0;

	}

//	l = i;

	uda.clear();

	int ans = bfs(1);

	printf("%d\n",ans);

	return 0;

}