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.Collections.Generic;
using System.Linq;
using static System.Console;
using static System.Convert;
using static System.Math;
//using Debug;
//using static System.Globalization.CultureInfo;
//using Edge = Pair<long, int>;
using System.Text;
class Program
{
    static void Main(string[] args)
    {
        Solve();
        //WriteLine(Solve());
    }
    static void Solve()
    {
        var nm = Input.ar; if (nm[1] == 0) { WriteLine(-1);return; }
        var edge = Enumerable.Repeat(0, nm[0]).Select(_ => new Dictionary<int, List<Pair<int, int,int>>>()).ToArray();
        var pp = Enumerable.Repeat(0, nm[0]).Select(_ => new Dictionary<int, List<int>>()).ToArray();
        for(var i=0;i<nm[1];i++)
        {
            var pqc = Input.ar;
            pqc[0]--;--pqc[1];
            for (var j = 0; j < 2; j++)
                if (pp[pqc[j]].ContainsKey(pqc[2]))
                    pp[pqc[j]][pqc[2]].Add(pqc[1 - j]);
                else pp[pqc[j]][pqc[2]] = new List<int> { pqc[1 - j] };
        }
        for(var i = 0; i < nm[0]; i++)
        {
            edge[i][-1] = new List<Pair<int, int, int>>();
            foreach (var d in pp[i])
            {
                if (!edge[i].ContainsKey(d.Key))
                    edge[i][d.Key] = new List<Pair<int, int, int>>();
                edge[i][d.Key].AddRange(d.Value.Select(v => new Pair<int, int, int>(0, v, d.Key)));
                edge[i][-1].AddRange(d.Value.Select(v => new Pair<int, int, int>(1, v, -1)));
                edge[i][d.Key].Add(new Pair<int, int, int>(0, -1, d.Key));
            }
        }
        var dist = Enumerable.Repeat(0, nm[0]).Select(_ => new Dictionary<int, int>()).ToArray();
        var deq = new Deque<Pair<int,int, int>>(5 * nm[1]);
        deq.EnqueueHead(new Pair<int, int, int>(0, 0, -1));
        dist[0][-1] = 0;
        while (deq.Count != 0)
        {
            var p = deq.DequeueHead();
            foreach (var ad in edge[p.v2][p.v3])
                if (!dist[ad.v2].ContainsKey(ad.v3) || dist[ad.v2][ad.v3] > dist[p.v2][p.v3] + ad.v1)
                {
                    dist[ad.v2][ad.v3] = dist[p.v2][p.v3] + ad.v1;
                    if (ad.v1 == 0)
                        deq.EnqueueHead(new Pair<int, int, int>(dist[ad.v2][ad.v3], ad.v2, ad.v3));
                    else deq.EnqueueTail(new Pair<int, int, int>(dist[ad.v2][ad.v3], ad.v2, ad.v3));
                }
        }
        WriteLine(dist[nm[0] - 1].ContainsKey(-1) ? dist[nm[0] - 1][-1] : -1);
    }
}

public class Input
{
    public static string read => ReadLine().Trim();
    public static int[] ar => read.Split(' ').Select(int.Parse).ToArray();
    public  static int num => ToInt32(read);
    public static long[] arL => read.Split(' ').Select(long.Parse).ToArray();
    public  static long numL => ToInt64(read);
    public static char[][] gred(int h) 
        => Enumerable.Repeat(0, h).Select(_ => read.ToCharArray()).ToArray();
    public static int[] ar1D(int n)
        => Enumerable.Repeat(0, n).Select(_ => num).ToArray();
    public static long[] arL1D(int n)
        => Enumerable.Repeat(0, n).Select(_ => numL).ToArray();
    public static string[] strs(int n)
        => Enumerable.Repeat(0, n).Select(_ => read).ToArray();
    public static int[][] ar2D(int n)
        => Enumerable.Repeat(0, n).Select(_ => ar).ToArray();
    public static long[][] arL2D(int n)
        => Enumerable.Repeat(0, n).Select(_ => arL).ToArray();
    public static T getValue<T>(string g)
    {
        var t = typeof(T);
        if (t == typeof(int))
            return (T)(object)int.Parse(g);
        if (t == typeof(long))
            return (T)(object)long.Parse(g);
        if (t == typeof(string))
            return (T)(object)g;
        if (t == typeof(char))
            return (T)(object)char.Parse(g);
        if (t == typeof(double))
            return (T)(object)double.Parse(g);
        if (t == typeof(bool))
            return (T)(object)bool.Parse(g);
        return default(T);
    }
    public const long Inf = (long)1e18;
    public const double eps = 1e-6;
    public  const string Alfa = "abcdefghijklmnopqrstuvwxyz";
    public  const int MOD = 1000000007;
}
public class Pair<T1, T2> : IComparable<Pair<T1, T2>>
{
    public T1 v1 { get; set; }
    public T2 v2 { get; set; }
    public Pair() : this(default(T1), default(T2)) { }
    public Pair(T1 v1, T2 v2)
    { this.v1 = v1;this.v2 = v2; }

    public int CompareTo(Pair<T1, T2> p)
    {
        var c = Comparer<T1>.Default.Compare(v1, p.v1);
        if (c == 0)
            c = Comparer<T2>.Default.Compare(v2, p.v2);
        return c;
    }
    public static Pair<T1, T2> MakePair()
    {
        var r = ReadLine().Split(' ');
        return new Pair<T1, T2>(Input.getValue<T1>(r[0]), Input.getValue<T2>(r[1]));
    }
    public override string ToString()
        => $"{v1.ToString()} {v2.ToString()}";
}

public class Pair<T1, T2, T3> : Pair<T1, T2>, IComparable<Pair<T1, T2, T3>>
{
    public T3 v3 { get; set; }
    public Pair() : base() { v3 = default(T3); }
    public Pair(T1 v1, T2 v2, T3 v3) : base(v1,v2)
    { this.v3 = v3; }
    
    public int CompareTo(Pair<T1, T2, T3> p)
    {
        var c = base.CompareTo(p);
        if (c == 0)
            c = Comparer<T3>.Default.Compare(v3, p.v3);
        return c;
    }
    public new static Pair<T1, T2, T3> MakePair()
    {
        var r = ReadLine().Split(' ');
        return new Pair<T1, T2, T3>(Input.getValue<T1>(r[0]), Input.getValue<T2>(r[1]), Input.getValue<T3>(r[2]));
    }
    public override string ToString()
        => $"{base.ToString()} {v3.ToString()}";
}
public class Deque<T>
{
    private RingBuffer<T> _buf;
    int _offset = 0, _size;
    public int Count { get; private set; }
    public Deque(int size) { _buf = new RingBuffer<T>(_size = size); }
    public T this[int index]
    {
        get { return _buf[index + _offset]; }
        set { _buf[index + _offset] = value; }
    }
    public void EnqueueHead(T item)
    {
        _buf[--_offset] = item;
        Count++;
    }
    public T DequeueHead()
    {
        if (Count == 0) return default(T);
        Count--;
        return _buf[_offset++];
    }
    public void EnqueueTail(T item)
    {
        _buf[Count++ + _offset] = item;
    }
    public T DequeueTail()
    {
        if (Count == 0) return default(T);
        return _buf[--Count + _offset];
    }
    public T PeekHead { get { return _buf[_offset]; } }
    public T PeekTail { get { return _buf[_offset + Count - 1]; } }
    public T[] ToArray()
    {
        var t = new T[Count];
        for (var i = 0; i < Count; i++)
            t[i] = _buf[i + _offset];
        return t;
    }
}

public class RingBuffer<T>
{
    private T[] _item;
    public int Count { get; private set; }
    public RingBuffer(int size)
    {
        _item = new T[Count = size];
    }
    public T this[int index]
    {
        get { index %= Count; if (index < 0) index += Count; return _item[index]; }
        set {index %= Count;if (index < 0) index += Count; _item[index] = value; }
    }
}