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# (Mono 4.6.2.0) (judge verdict: Time Limit Exceeded)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class TEST{
	static void Main(){
		Sol mySol =new Sol();
		mySol.Solve();
	}
}

class Sol{
	public void Solve(){
		
		Dictionary<Pair, List<Pair2>> E = new Dictionary<Pair, List<Pair2>>();
		int Ext = (int) 1e7;
		
	
		for(int i=0;i<M;i++){
			var p0 = new Pair(P[i],C[i]);
			var q0 = new Pair(Q[i],C[i]);
			var p1 = new Pair(P[i],Ext);
			var q1 = new Pair(Q[i],Ext);
			if(!E.ContainsKey(p0)) E.Add(p0, new List<Pair2>());
			if(!E.ContainsKey(q0)) E.Add(q0, new List<Pair2>());
			if(!E.ContainsKey(p1)) E.Add(p1, new List<Pair2>());
			if(!E.ContainsKey(q1)) E.Add(q1, new List<Pair2>());
			E[p0].Add(new Pair2(q0, 0));
			E[q0].Add(new Pair2(p0, 0));
			E[p0].Add(new Pair2(p1, 1));// 外に出るときだけコスト1
			E[q0].Add(new Pair2(q1, 1));
			E[p1].Add(new Pair2(p0, 0));
			E[q1].Add(new Pair2(q0, 0));
		}
		
		long Inf = (long) 1e9;
		Dictionary<Pair, long> Min = new Dictionary<Pair, long>();
		var SH = new SkewHeap<Pair2>();
		var pstart = new Pair(0,Ext);
		Min.Add(pstart, 0);
		SH.Push(new Pair2(pstart, 0));
		
		while(SH.Count > 0){
			var now = SH.Top; SH.Pop();
			var p = now.Pos;
			var c = now.Cost;
			Min[p] = c;
			if(!E.ContainsKey(p)) continue;
			foreach(var nxt in E[p]){
				var np = nxt.Pos;
				var nc = nxt.Cost;
				if(!Min.ContainsKey(np)) Min.Add(np, Inf);
				if(Min[np] <= c + nc) continue;
				Min[np] = c + nc;
				SH.Push(new Pair2(np, c + nc));
			}
		}
		
		var pend = new Pair(N-1, Ext);
		Console.WriteLine(Min.ContainsKey(pend) ? Min[pend] : -1);
		
		
		
	}
	int N,M;
	int[] P,Q,C;
	
	struct Pair{
		int P;
		int C;
		public Pair(int p, int c){
			P = p; C = c;
		}
	}
	
	class Pair2 : IComparable<Pair2> {
		public Pair Pos;
		public long Cost;
		public Pair2(Pair p, long c) {
			Pos = p; Cost = c;
		}
		public int CompareTo(Pair2 t) {
			//return this.Cost > t.Cost ? -1 : this.Cost < t.Cost ? 1 : 0;
			return this.Cost > t.Cost ? 1 : this.Cost < t.Cost ? -1 : 0;
		}
	}
	public Sol(){
		var d = ria();
		N = d[0]; M = d[1];
		P = new int[M];
		Q = new int[M];
		C = new int[M];
		for(int i=0;i<M;i++){
			d = ria();
			P[i] = d[0] - 1; Q[i] = d[1] - 1; C[i] = d[2];
		}
	}

	static String rs(){return Console.ReadLine();}
	static int ri(){return int.Parse(Console.ReadLine());}
	static long rl(){return long.Parse(Console.ReadLine());}
	static double rd(){return double.Parse(Console.ReadLine());}
	static String[] rsa(char sep=' '){return Console.ReadLine().Split(sep);}
	static int[] ria(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>int.Parse(e));}
	static long[] rla(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>long.Parse(e));}
	static double[] rda(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>double.Parse(e));}
}




class SkewHeap<T> where T:IComparable<T>{
	public int Count{
		get{return cnt;}
		private set{cnt=value;}
	}
	
	public SkewHeap(){
		root=null;
		this.Count=0;
	}
	
	public void Push(T v){
		NodeSH<T> p=new NodeSH<T>(v);
		root=NodeSH<T>.Meld(root,p);
		this.Count++;
	}
	
	public void Pop(){
		if(root==null)return;
		root=NodeSH<T>.Meld(root.L,root.R);
		this.Count--;
	}
	
	public T Top{
		get{return root.Val;}
	}
	
	int cnt;
	NodeSH<T> root;
	
	class NodeSH<S> where S : IComparable<S> {
		public NodeSH<S> L, R;
		public S Val;

		public NodeSH(S v){
			Val = v;
			L = null; R = null;
		}
		public static NodeSH<S> Meld(NodeSH<S> a, NodeSH<S> b){
			if(a == null)return b;
			if (b == null) return a;
			if (a.Val.CompareTo(b.Val) > 0) swap(ref a, ref b);
			a.R = Meld(a.R, b);
			swap(ref a.L, ref a.R);
			return a;
		}

		static void swap<U>(ref U x, ref U y) {
			U t = x; x = y; y = t;
		}
	}
}