Problem:
Score : 600 points

Problem Statement

Snuke's town has a subway system, consisting of N stations and M railway lines. The stations are numbered 1 through N. Each line is operated by a company. Each company has an identification number.

The i-th ( 1 \leq i \leq M ) line connects station p_i and q_i bidirectionally. There is no intermediate station. This line is operated by company c_i.

You can change trains at a station where multiple lines are available.

The fare system used in this subway system is a bit strange. When a passenger only uses lines that are operated by the same company, the fare is 1 yen (the currency of Japan). Whenever a passenger changes to a line that is operated by a different company from the current line, the passenger is charged an additional fare of 1 yen. In a case where a passenger who changed from some company A's line to another company's line changes to company A's line again, the additional fare is incurred again.

Snuke is now at station 1 and wants to travel to station N by subway. Find the minimum required fare.

Constraints

2 \leq N \leq 10^5

0 \leq M \leq 2×10^5

1 \leq p_i \leq N (1 \leq i \leq M)

1 \leq q_i \leq N (1 \leq i \leq M)

1 \leq c_i \leq 10^6 (1 \leq i \leq M)

p_i \neq q_i (1 \leq i \leq M)

Input

The input is given from Standard Input in the following format:

N M
p_1 q_1 c_1
:
p_M q_M c_M

Output

Print the minimum required fare. If it is impossible to get to station N by subway, print -1 instead.

Sample Input 1

3 3
1 2 1
2 3 1
3 1 2

Sample Output 1

1

Use company 1's lines: 1 → 2 → 3. The fare is 1 yen.

Sample Input 2

8 11
1 3 1
1 4 2
2 3 1
2 5 1
3 4 3
3 6 3
3 7 3
4 8 4
5 6 1
6 7 5
7 8 5

Sample Output 2

2

First, use company 1's lines: 1 → 3 → 2 → 5 → 6. Then, use company 5's lines: 6 → 7 → 8. The fare is 2 yen.

Sample Input 3

2 0

Sample Output 3

-1

Solution in C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include<ctime>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cmath> 
#include<cstring> 
#include<cassert>
#include<string>
#include<sstream>
#include<fstream>
#include<deque>
#include<queue>
#include<vector>
#include<map>
#include<list>
#include<stack>
#include<set>
#include<bitset>
#include<iomanip>
#include<utility>
#include<functional>
#include<cctype>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<climits>
#include<clocale>
#include<complex>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cwchar>
#include<cwctype>
#include<exception>
#include<locale>
#include<numeric>
#include<new>
#include<stdexcept>
#include<limits>
using namespace std;

#define ll long long
#define INF 1e9
#define rep(i,n) for(int (i)=0;(i)<n;i++)
#define REP(i,n) for(int (i)=1;(i)<=n;i++)
#define mk(a,b) make_pair(a,b)
#define fi first
#define se second
#define pii pair<int,int>
#define sz(s) s.size()
#define all(s) (s.begin(),s.end())

const int maxn=100005;
int n,m,cnt;
vector<pii>edge[maxn*4],col[maxn*10];
int dist[maxn*2],f[maxn],in[maxn],id[maxn];

int Find(int x){
	return f[x]==x?x:f[x]=Find(f[x]);
}

void Union(int x,int y){
	x=Find(x);y=Find(y);
	if(x!=y)f[x]=y;
}

void Dij(){
	REP(i,4*n)dist[i]=INF;
	dist[1]=0;
	priority_queue<pii,vector<pii>,greater<pii> >q;
	q.push(mk(0,1));
	while(!q.empty()){
		pii x=q.top();
		q.pop();
		if(dist[x.se]<x.fi)continue;
		rep(i,edge[x.se].size()){
			pii y=edge[x.se][i];
			if(dist[x.se]+y.se<dist[y.fi]){
				dist[y.fi]=dist[x.se]+y.se;
				q.push(mk(dist[y.fi],y.fi));
			}
		}
	}
}

int main(){
	rep(i,maxn)f[i]=i;
	scanf("%d%d",&n,&m);
	rep(i,m){
		int u,v,c;
		scanf("%d%d%d",&u,&v,&c);
		col[c].push_back(mk(u,v));
		edge[u].push_back(mk(v,2));
		edge[v].push_back(mk(u,2));
	}
	cnt=n;
	REP(i,1000000){
		if(col[i].empty())continue;
		vector<int>occ;
		rep(j,col[i].size()){
			pii e=col[i][j];
			Union(e.fi,e.se);
			occ.push_back(e.fi);
			occ.push_back(e.se);
		}
		sort(occ.begin(),occ.end());
		occ.resize(unique(occ.begin(),occ.end())-occ.begin());
		rep(i,occ.size()){
			int x=occ[i];
			if(Find(x)==x)in[x]=++cnt;
		}
		rep(i,occ.size()){
			int x=occ[i];
			edge[in[Find(x)]].push_back(mk(x,1));
			edge[x].push_back(mk(in[Find(x)],1));
		}
		rep(i,occ.size())f[occ[i]]=occ[i];
	}
	Dij();
	if(dist[n]==INF)printf("-1");
	else printf("%d",dist[n]/2);
	return 0;
}