using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace A
{
    partial class Program
    {
        public IEnumerable<string> Solve()
        {
            var n = Num;
            var m = Num;

            var edges = new List<List<long>>();
            var dists = new List<long>();

            for (int i = 0; i < n; i++)
            {
                dists.Add(-1);
                dists.Add(-1);
                dists.Add(-1);
                edges.Add(new List<long>());
            }

            for (int i = 0; i < m; i++)
            {
                var e1 = Num - 1;
                var e2 = Num - 1;
                edges[(int)e1].Add(e2);
            }
            var src = Num - 1;
            var dest = Num - 1;

            dists[(int)src] = 0;

            var checkeds = new HashSet<long>();
            var checkings = new HashSet<long>() { src };

            while (checkings.Count > 0)
            {
                var newCheckings = new HashSet<long>();

                foreach (var c in checkings)
                {
                    var newD = dists[(int)c] + 1;

                    foreach (var t in edges[(int)(c % n)])
                    {
                        var lay = c / n;
                        var tt = ((lay + 1) % 3) * n + t;

                        if (checkeds.Contains(tt))
                            continue;

                        if (dists[(int)tt] < 0)
                            dists[(int)tt] = newD;
                        else
                            dists[(int)tt] = Math.Min(dists[(int)tt], newD);

                        if (!newCheckings.Contains(tt))
                            newCheckings.Add(tt);
                    }
                }

                foreach (var c in checkings)
                    checkeds.Add(c);
                checkings = newCheckings;
            }

            var res = dists[(int)dest];
            if (res < 0)
                yield return "-1";
            else
                yield return (res / 3).ToString();
        }
    }

    class StartingPoint
    {
        static void Main(string[] args)
        {
            if (args?.Any() == true)
            {
                var assembly = Assembly.GetExecutingAssembly();
                var resourceName = "AtCoder.Resources.In.txt";
                var stream = assembly.GetManifestResourceStream(resourceName);
                var streamReader = new StreamReader(stream, Encoding.UTF8);
                var str = streamReader.ReadToEnd();
                var textReader = new StringReader(str);
                Console.SetIn(textReader);
            }

            try
            {
                foreach (var s in new Program().Solve())
                {
                    Console.WriteLine(s);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                if (args?.Any() != true)
                    throw e;
            }

            if (args?.Any() == true)
                Console.ReadLine();
        }
    }

    partial class Program
    {
        long[] Nums(long count) => Enumerable.Range(0, (int)count).Select(_ => Num).ToArray();
        long Num => long.Parse(Str);
        double NumD => (double)Num;

        StringBuilder sb = new StringBuilder();

        string Str => GetStr();
        string GetStr()
        {
            sb.Clear();

            int r = 0;
            bool? b = null;
            do
            {
                r = Console.Read();
                b = IsBreak(r);
            } while (b == true);

            if (b == null)
                throw new Exception("input error: unexpected end of stream");

            do
            {
                sb.Append((char)r);
                r = Console.Read();
                b = IsBreak(r);
            } while (b == false);

            return sb.ToString();
        }

        bool? IsBreak(int c)
        {
            switch (c)
            {
                case -1:
                    return null;
                case (int)' ':
                case (int)'\n':
                case (int)'\r':
                    return true;
                default:
                    return false;
            }
        }
    }
}

Measured memory_bytes: 48888