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 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <stdio.h>
#include <stdlib.h>
#define ll long long
#define rep(i,l,r)for(ll i=(l);i<(r);i++)
#define repp(i,l,r,k)for(ll i=(l);i<(r);i+=(k))
#define INF ((1LL<<62)-(1LL<<31))

typedef struct edge{ll s,g,c;}E;
typedef struct vertex{ll eki,rosen;}V;

int edgehikaku(const void*a,const void*b){
	E*p=(E*)a,*q=(E*)b;
	if((*p).s<(*q).s)return -1;
	if((*p).s>(*q).s)return  1;
	if((*p).g<(*q).g)return -1;
	return 1;
}


int vsort(const void*a,const void*b){
	V*p=(V*)a,*q=(V*)b;
	if((*p).eki<(*q).eki)return -1;
	if((*p).eki>(*q).eki)return  1;
	if((*p).rosen<(*q).rosen)return -1;
	return 1;
}
int unique(V*a,int cnt){
	qsort(a,cnt,sizeof(V),vsort);
	int c=0;
	rep(i,1,cnt)if(a[i].eki!=a[c].eki||a[i].rosen!=a[c].rosen)a[++c]=a[i];
	return c+1;
}



//プラキュー（二分ヒープ）（優先度変更ありバージョン）
ll heapN,heap[1210000],heapinv[1210000];
int PQhikaku(int i,int j);//jの方が優先度が高いならtrueを返す
void PQchange(int n);
void heap_utod(int n){
	if(2*n>heapN)return;
	int rflag=(2*n+1<=heapN)&&(PQhikaku(2*n,2*n+1));
	if(PQhikaku(n,2*n+rflag)){
		ll temp=heap[2*n+rflag];
		heap[2*n+rflag]=heap[n];
		heap[n]=temp;
		heapinv[heap[n]]=n;
		heapinv[heap[2*n+rflag]]=2*n+rflag;	
		heap_utod(2*n+rflag);
	}
}
void heap_dtou(int n){
	if(n==1||PQhikaku(n,n/2))return;
	ll temp=heap[n];
	heap[n]=heap[n/2];
	heap[n/2]=temp;
	heapinv[heap[n]]=n;
	heapinv[heap[n/2]]=n/2;
	heap_dtou(n/2);
}
ll PQpop(){
	ll rr=heap[1];
	heapinv[heap[1]]=0;
	heap[1]=heap[heapN--];
	heapinv[heap[1]]=1;
	heap_utod(1);
	return rr;
}
void PQpush(ll n){
	heap[++heapN]=n;
	heapinv[heap[heapN]]=heapN;
	heap_dtou(heapN);
}

//頂点数、スタート、辺の情報、辺の数を引いて最短距離と経路を返す
//辺は有向辺なので、無向なら両方の向きを入れること
//O((E+V)logV)
ll daikusutorappp[1<<21];
void daikusutora2(ll n,ll s,E*e,ll ecnt){
	qsort(e,ecnt,sizeof(E),edgehikaku);

	ll*es=(ll*)malloc(n*sizeof(ll));
	es[0]=0;
	ll t=0;
	rep(i,1,n){
		while(t<ecnt&&e[t].s<i)t++;
		es[i]=t;
	}
	rep(i,0,n)daikusutorappp[i]=i==s?0:INF;
	rep(i,0,n)PQpush(i);
	while(heapN){
		ll mv=PQpop();
		ll t=es[mv];
		while(e[t].s==mv){
			if(daikusutorappp[e[t].g]>daikusutorappp[mv]+e[t].c){
				daikusutorappp[e[t].g]=daikusutorappp[mv]+e[t].c;
				PQchange(e[t].g);
			}
			t++;
		}
	}
	free(es);
}
int PQhikaku(int i,int j){return daikusutorappp[heap[i]]>daikusutorappp[heap[j]];}
void PQchange(int n){if(heapinv[n])heap_dtou(heapinv[n]);}



E e[800010];
V v[1200010];
ll vcnt,ecnt;

int bs(int eki,int rosen){
	int l=0,r=vcnt;
	while(r-l>1){
		int m=(l+r)/2;
		if(v[m].eki<eki||(v[m].eki==eki&&v[m].rosen<=rosen))l=m;
		else r=m;
	}
	return l;
}

int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	rep(i,0,m){
		int p,q,c;
		scanf("%d%d%d",&p,&q,&c);
		v[vcnt].eki=p;v[vcnt].rosen=c;vcnt++;
		v[vcnt].eki=q;v[vcnt].rosen=c;vcnt++;
		v[vcnt].eki=p;v[vcnt].rosen=-1;vcnt++;
		v[vcnt].eki=q;v[vcnt].rosen=-1;vcnt++;
		
		//これはいちほ
		e[ecnt].s=p;e[ecnt].g=q;e[ecnt].c=c;ecnt+=6;
	}

	v[vcnt].eki=1;v[vcnt].rosen=-1;vcnt++;
	v[vcnt].eki=n;v[vcnt].rosen=-1;vcnt++;
	vcnt=unique(v,vcnt);
	
	//辺を張り直す
	repp(i,0,ecnt,6){
		int p=e[i].s,q=e[i].g,c=e[i].c;
		int v1=bs(p,-1);
		int v2=bs(p,c);
		int v3=bs(q,c);
		int v4=bs(q,-1);
		e[i  ].s=v1;e[i  ].g=v2;e[i  ].c=1;
		e[i+1].s=v2;e[i+1].g=v1;e[i+1].c=0;
		e[i+2].s=v2;e[i+2].g=v3;e[i+2].c=0;
		e[i+3].s=v3;e[i+3].g=v2;e[i+3].c=0;
		e[i+4].s=v3;e[i+4].g=v4;e[i+4].c=0;
		e[i+5].s=v4;e[i+5].g=v3;e[i+5].c=1;
	}
	
	daikusutora2(vcnt,0,e,ecnt);
	ll ans=daikusutorappp[bs(n,-1)];
	printf("%lld\n",ans==INF?-1:ans);
	return 0;
}