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: Wrong Answer)


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <iostream>
#include <string>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for ( int i=1; i<=int(n); i++ )
#define MP make_pair
#define PB push_back
#define SZ(x) (int((x).size()))
#define ALL(x) (x).begin(), (x).end()
#define X first
#define Y second
template<typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

template <class T>
inline bool RD(T &ret) {
        char c; int sgn;
        if (c = getchar(), c == EOF) return 0;
        while (c != '-' && (c<'0' || c>'9')) c = getchar();
        sgn = (c == '-') ? -1 : 1 , ret = (c == '-') ? 0 : (c - '0');
        while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
        ret *= sgn;
        	return 1;
}
template <class T>
inline void PT(T x) {
        if (x < 0) putchar('-') ,x = -x;
        if (x > 9) PT(x / 10);
        putchar(x % 10 + '0');
}
typedef unsigned long long LL;
typedef pair<int, int> pii;
typedef long double LD;
const int INF = 0x3f3f3f3f;


const int N = 2e5 + 100;
struct node {
	int v, col;
	node(int v = 0, int col = 0):v(v), col(col){}
};
bool cmp(node a, node b) {
	return a.col < b.col;
}
vector<node> G[N];
bool vis[N];

int ans[N];
int que[N], t;
int s;
int q[N], tail;
int n;
int idx[N], id;
void BFS(int col) {
    for(int i = 0; i < tail; i ++) {
        int u = q[i];
        if(u == n) return ;
        int lb = -1, ub = SZ(G[u]);
        while(ub - lb > 1) {
            int mid = (lb + ub) >> 1;
            if(G[u][mid].col >= col) ub = mid;
            else lb = mid;
        }
        if(ub == SZ(G[u]) || G[u][ub].col != col) continue;
        for(int i = ub; i < SZ(G[u]); i ++) {
            if(G[u][i].col != col) break;
            int v = G[u][i].v;
            if(ans[v] <= ans[s] || idx[v] == id) continue;
            if(ans[v] == INF) ans[v] = ans[s] + 1, que[t ++] = v;
            q[tail ++] = v;
            idx[v] = id;
        }
    }
}

int sta[N], top;
int main() {
	memset(ans, 0x3f, sizeof(ans));
	int m;
	RD(n), RD(m);
	for(int i = 1;  i <= m; i ++) {
		int u, v, c;
		RD(u), RD(v), RD(c);
		G[u].PB(node(v, c));
		G[v].PB(node(u, c));
	}
	for(int i = 1; i <= n; i ++) sort(G[i].begin(), G[i].end(), cmp);
	ans[1] = 0;
	que[t ++] = 1;
	for(int i = 0; i < t; i ++) {
		int u = que[i];
		if(u == n) break;
		s = u;
		for(int i = 0; i < SZ(G[u]); i ++) {
			int v = G[u][i].v;
			if(ans[v] <= ans[u]) continue;
			if(vis[G[u][i].col]) continue;
			vis[G[u][i].col] = 1;
			sta[ ++ top ] = G[u][i].col;
			tail = 0;
			q[tail ++] = s;
			idx[s] = ++ id;
			BFS(G[u][i].col);
		}
		for(int i = 1; i <= top; i ++) vis[sta[i]] = 0;
		top = 0;
	}
	if(ans[n] == INF) puts("-1");
	else printf("%d\n", ans[n]);
}