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

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Diagnostics;
using Enu = System.Linq.Enumerable;

public class Program
{
    static readonly int INF = (int)1e9;

    public void Solve()
    {
        int N = Reader.Int(), M = Reader.Int();
        var E = new List<Edge>[N];

        for (int i = 0; i < N; i++)
            E[i] = new List<Edge>();
        for (int i = 0; i < M; i++)
        {
            int a = Reader.Int() - 1, b = Reader.Int() - 1, c = Reader.Int();
            E[a].Add(new Edge(b, c));
            E[b].Add(new Edge(a, c));
        }
        var deq = new Deque<Vertex>();
        deq.PushBack(new Vertex(0, -1, 0));
        var minCost = new int[N];
        for (int i = 1; i < N; i++) minCost[i] = INF;
        var candColors = new long[N][];
        var seenColors = new long[N][];
        for (int a = 0; a < N; a++)
        {
            candColors[a] = new long[N / 64 + 1];
            seenColors[a] = new long[N / 64 + 1];
            foreach (var e in E[a])
                candColors[a][e.color >> 6] |= 1L << e.color;
        }

        while (deq.Count > 0)
        {
            var v = deq.PopFront();
            int a = v.V, col = v.Color, cost = v.Cost;
            if (cost > minCost[a]) continue;
            if (cost >= minCost[N - 1]) break;

            foreach (var e in E[a])
            {
                int b = e.to, ec = e.color;
                if (ec == col)
                {
                    if (minCost[b] == INF ||
                        cost == minCost[b] &&
                        (candColors[b][ec >> 6] >> ec & 1) == 1 &&
                        (seenColors[b][ec >> 6] >> ec & 1) == 0)
                    {
                        minCost[b] = cost;
                        seenColors[b][ec >> 6] |= 1L << ec;
                        deq.PushFront(new Vertex(b, ec, cost));
                    }
                }
                else if (minCost[b] == INF ||
                         cost + 1 == minCost[b] &&
                         (candColors[b][ec >> 6] >> ec & 1) == 1 &&
                         (seenColors[b][ec >> 6] >> ec & 1) == 0)
                {
                    minCost[b] = cost + 1;
                    seenColors[b][ec >> 6] |= 1L << ec;
                    deq.PushBack(new Vertex(b, ec, cost + 1));
                }
            }
        }

        Console.WriteLine(minCost[N - 1] == INF ? -1 : minCost[N - 1]);
    }


    struct Vertex
    {
        public int V, Color, Cost;
        public Vertex(int v, int col, int cost) { V = v; Color = col; Cost = cost; }
    }

    struct Edge
    {
        public int to;
        public int color;
        public Edge(int to, int c)
        {
            this.to = to; this.color = c;
        }
    }

    class Deque<T>
    {
        private T[] A = new T[1000];
        private int L;
        public int Count { get; private set; }

        public T PeekFront() { return A[L]; }
        public T PeekBack() { return A[(L + Count - 1) % A.Length]; }
        public void PushFront(T val)
        {
            if (Count++ == A.Length) DoubleArray();
            L = (L + A.Length - 1) % A.Length;
            A[L] = val;
        }
        public void PushBack(T val)
        {
            if (Count == A.Length) DoubleArray();
            A[(L + Count++) % A.Length] = val;
        }
        public T PopFront()
        {
            if (Count-- == 0) throw new InvalidOperationException("empty");
            T res = A[L];
            L = (L + 1) % A.Length;
            return res;
        }
        public T PopBack()
        {
            if (Count-- == 0) throw new InvalidOperationException("empty");
            return A[(L + Count) % A.Length];
        }
        public void DoubleArray()
        {
            T[] next = new T[A.Length * 2];
            Array.Copy(A, L, next, 0, A.Length - L);
            Array.Copy(A, 0, next, A.Length - L, L);
            L = 0;
            A = next;
        }
    }
}

class Entry { static void Main() { new Program().Solve(); } }
class Reader
{
    static TextReader reader = Console.In;
    static readonly char[] separator = { ' ' };
    static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries;
    static string[] A = new string[0];
    static int i;
    static void Init() { A = new string[0]; }
    public static void Set(TextReader r) { reader = r; Init(); }
    public static void Set(string file) { reader = new StreamReader(file); Init(); }
    public static bool HasNext() { return CheckNext(); }
    public static string String() { return Next(); }
    public static int Int() { return int.Parse(Next()); }
    public static long Long() { return long.Parse(Next()); }
    public static double Double() { return double.Parse(Next()); }
    public static int[] IntLine() { return Array.ConvertAll(Split(Line()), int.Parse); }
    public static int[] IntArray(int N) { return Range(N, Int); }
    public static int[][] IntTable(int H) { return Range(H, IntLine); }
    public static string[] StringArray(int N) { return Range(N, Next); }
    public static string[][] StringTable(int N) { return Range(N, () => Split(Line())); }
    public static string Line() { return reader.ReadLine().Trim(); }
    static string[] Split(string s) { return s.Split(separator, op); }
    static T[] Range<T>(int N, Func<T> f) { var r = new T[N]; for (int i = 0; i < N; r[i++] = f()) ; return r; }
    static string Next() { CheckNext(); return A[i++]; }
    static bool CheckNext()
    {
        if (i < A.Length) return true;
        string line = reader.ReadLine();
        if (line == null) return false;
        if (line == "") return CheckNext();
        A = Split(line);
        i = 0;
        return true;
    }
}