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<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 (!edge[pqc[j]].ContainsKey(pqc[2]))
                    edge[pqc[j]].Add(pqc[2], new List<int> { pqc[1 - j] });
                else edge[pqc[j]][pqc[2]].Add(pqc[1 - j]);
            }
        }
        var dist = Enumerable.Repeat(0, nm[0]).Select(_ => new Dictionary<int, int>()).ToArray();
        var deq = new Deque<Pair<int, int>>(5 * nm[1]);
        deq.EnqueueHead(new Pair<int, int>(0,0));
        dist[0][0] = 0;
        while (deq.Count != 0)
        {
            var p = deq.DequeueHead();
            if (p.v2 == 0)
            {
                foreach (var c in edge[p.v1].Keys)
                    if (!dist[p.v1].ContainsKey(c) || dist[p.v1][c] > dist[p.v1][0] + 1)
                    {
                        dist[p.v1][c] = dist[p.v1][0] + 1;
                        deq.EnqueueTail(new Pair<int, int>(p.v1, c));
                    }continue;
            }
            if(!dist[p.v1].ContainsKey(0)||dist[p.v1][0]>dist[p.v1][p.v2])
            {
                dist[p.v1][0] = dist[p.v1][p.v2];
                deq.EnqueueHead(new Pair<int, int>(p.v1, 0));
            }
            foreach (var ad in edge[p.v1][p.v2])
                if (!dist[ad].ContainsKey(p.v2) || dist[ad][p.v2] > dist[p.v1][p.v2])
                {
                    dist[ad][p.v2] = dist[p.v1][p.v2];
                    deq.EnqueueHead(new Pair<int, int>(ad, p.v2));
                }
        }
        WriteLine(dist[nm[0] - 1].ContainsKey(0) ? dist[nm[0] - 1][0] : -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 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; }
    }
}

Measured memory_bytes: 141760