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 System.Text;
class Program
{
    static void Main(string[] args)
    {
        Solve();
        //WriteLine(Solve());
    }
    class G
    {
        public int index;
        public List<int> adj;
        public Dictionary<int, List<int>> dist;
        public int Weight = int.MaxValue;
        public G(int index)
        {
            this.index = index;
            adj = new List<int>();
            dist = new Dictionary<int, List<int>>();
        }
    }
    static void Solve()
    {
        var nm = Input.ar;
        var gs = Enumerable.Range(0, nm[0]).Select(v => new G(v)).ToArray();
        for(var i = 0; i < nm[1]; i++)
        {
            var pqc = Input.ar;
            for(var j = 0; j < 2; j++)
            {
                gs[pqc[j] - 1].adj.Add(pqc[1 - j] - 1);
                if (!gs[pqc[j] - 1].dist.ContainsKey(pqc[1 - j] - 1))
                    gs[pqc[j] - 1].dist[pqc[1 - j] - 1] = new List<int>();
                gs[pqc[j] - 1].dist[pqc[1 - j] - 1].Add(pqc[2]);
            }
        }
        var deq = new Deque<Tuple<G,int>>(nm[1]+nm[0]);
        gs[0].Weight = 0;
        foreach (var ad in gs[0].adj)
        {
            gs[ad].Weight = 1;
            foreach (var a in gs[0].dist[ad])
                deq.EnqueueTail(new Tuple<G, int>(gs[ad], a));
        }
        while (deq.Count != 0)
        {
            var g = deq.DequeueHead();
            foreach (var ad in g.Item1.adj)
                foreach (var a in g.Item1.dist[ad])
                {
                    var d = ToInt32(a != g.Item2);
                    if (gs[ad].Weight != int.MaxValue) continue;
                    gs[ad].Weight = g.Item1.Weight + d;
                    if (d == 0) deq.EnqueueHead(new Tuple<G, int>(gs[ad], a));
                    else deq.EnqueueTail(new Tuple<G, int>(gs[ad], a));
                }
        }
        WriteLine(gs[nm[0] - 1].Weight == int.MaxValue ? -1 : gs[nm[0] - 1].Weight);
    }
}

public class Input
{
    public static string read => ReadLine().Trim();
    public  static int[] ar => Array.ConvertAll(read.Split(' '), int.Parse);
    public  static int num => ToInt32(read);
    public static long[] arL => Array.ConvertAll(read.Split(' '), long.Parse);
    public  static long numL => ToInt64(read);
    public static char[][] gred(int h) 
        => Enumerable.Repeat(0, h).Select(v => read.ToCharArray()).ToArray();
    public static int[][] ar2D(int num)
        => Enumerable.Repeat(0, num).Select(_ => ar).ToArray();
    public static long[][] arL2D(int num)
        => Enumerable.Repeat(0, num).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 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 class RingBuffer<T>
{
    private T[] _item;
    private int _size;
    public RingBuffer(int size)
    {
        _size = size;
        _item = new T[size];
    }
    public T this[int index]
    {
        get { index = index < 0 ? (_size + index % _size)%_size : index % _size; return _item[index]; }
        set { index = index < 0 ? (_size + index % _size)%_size : index % _size; _item[index] = value; }
    }
}