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

}

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

#include <iostream>

#include <cstring>

#include <cstdio>

#include <queue>

#define N 200005

using namespace std;



int n, m, cnt, tot, id[N], head[N << 3], dis[N << 3]; 

struct node

{

	int u, v, w;

	bool operator < (const node &p) const

		{

			return w < p.w; 

		}

} a[N];

struct edge { int to, next, cost; } e[N << 3];

bool vis[N << 3]; 



template < typename T >

inline T read()

{

	T x = 0, w = 1; char c = getchar();

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

	while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();

	return x * w; 

}



inline void adde(int u, int v, int w) { e[++cnt] = (edge) { v, head[u], w }; head[u] = cnt; }



void bfs()

{

	memset(dis, 0x3f, sizeof(dis)); dis[1] = 0;

	deque<int> q;

	q.push_front(1);

	while(!q.empty())

	{

		int u = q.front(); q.pop_front();

		if(vis[u]) continue; vis[u] = 1; 

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

		{

			int v = e[i].to;

			if(dis[v] > dis[u] + e[i].cost)

			{

				dis[v] = dis[u] + e[i].cost; 

				if(!e[i].cost) q.push_front(v);

				else q.push_back(v); 

			}

		}

	}

}



int main()

{

	tot = n = read <int> (), m = read <int> (); 

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

		a[i].u = read <int> (), a[i].v = read <int> (), a[i].w = read <int> ();

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

	int last = a[1].w;

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

		if(a[i].w != a[last].w)

		{

			for(int j = last; j < i; j++)

			{

				if(!id[a[j].u]) id[a[j].u] = ++tot;

				if(!id[a[j].v]) id[a[j].v] = ++tot;

				adde(a[j].u, id[a[j].u], 1), adde(id[a[j].u], a[j].u, 0);

				adde(a[j].v, id[a[j].v], 1), adde(id[a[j].v], a[j].v, 0);

				adde(id[a[j].u], id[a[j].v], 0), adde(id[a[j].v], id[a[j].u], 0);

			}

			for(int j = last; j < i; j++)

				id[a[j].u] = id[a[j].v] = 0; 

			last = i; 

		}

	bfs();

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

	return 0; 

}