Problem:
Score : 500 points

Problem Statement

Ken loves ken-ken-pa (Japanese version of hopscotch). Today, he will play it on a directed graph G.
G consists of N vertices numbered 1 to N, and M edges. The i-th edge points from Vertex u_i to Vertex v_i.

First, Ken stands on Vertex S. He wants to reach Vertex T by repeating ken-ken-pa. In one ken-ken-pa, he does the following exactly three times: follow an edge pointing from the vertex on which he is standing.

Determine if he can reach Vertex T by repeating ken-ken-pa. If the answer is yes, find the minimum number of ken-ken-pa needed to reach Vertex T. Note that visiting Vertex T in the middle of a ken-ken-pa does not count as reaching Vertex T by repeating ken-ken-pa.

Constraints

2 \leq N \leq 10^5

0 \leq M \leq \min(10^5, N (N-1))

1 \leq u_i, v_i \leq N(1 \leq i \leq M)

u_i \neq v_i (1 \leq i \leq M)

If i \neq j, (u_i, v_i) \neq (u_j, v_j).

1 \leq S, T \leq N

S \neq T

Input

Input is given from Standard Input in the following format:

N M
u_1 v_1
:
u_M v_M
S T

Output

If Ken cannot reach Vertex T from Vertex S by repeating ken-ken-pa, print -1.
If he can, print the minimum number of ken-ken-pa needed to reach vertex T.

Sample Input 1

4 4
1 2
2 3
3 4
4 1
1 3

Sample Output 1

2

Ken can reach Vertex 3 from Vertex 1 in two ken-ken-pa, as follows: 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 in the first ken-ken-pa, then 4 \rightarrow 1 \rightarrow 2 \rightarrow 3 in the second ken-ken-pa. This is the minimum number of ken-ken-pa needed.

Sample Input 2

3 3
1 2
2 3
3 1
1 2

Sample Output 2

-1

Any number of ken-ken-pa will bring Ken back to Vertex 1, so he cannot reach Vertex 2, though he can pass through it in the middle of a ken-ken-pa.

Sample Input 3

2 0
1 2

Sample Output 3

-1

Vertex S and Vertex T may be disconnected.

Sample Input 4

6 8
1 2
2 3
3 4
4 5
5 1
1 4
1 5
4 6
1 6

Sample Output 4

2

Solution in C# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Linq;
using System.Collections.Generic;
using static System.Console;
using System.Text;
using System.IO;
using static System.Math;
using System.Numerics;
using System.Runtime.InteropServices.ComTypes;
using System.Xml.Schema;
using System.ComponentModel.DataAnnotations;
using System.Security.Cryptography;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Linq.Expressions;
using System.Xml.XPath;

namespace AtCoder
{
    public class Program
    {
        public static void Main(string[] args)
        {
            //SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
            Solve();
            //Out.Flush();
            Read();
        }

        static void Solve()
        {
            var (N, M) = Slice2<long>();
            var graph = new long[N].Select(_ => new List<long>()).ToArray();
            for (var i = 0; i < M; ++i)
            {
                var (u, v) = Slice2<long>();
                graph[--u].Add(--v);
            }
            var (S, T) = Slice2<long>();
            var MAX = long.MaxValue;
            var dp = new long[N].Select(_ => new int[3].Select(__ => MAX).ToArray()).ToArray();
            dp[S - 1][0] = 0;
            var pq = new priority_queue<(long node, long dst, long p)>((l, r) => r.dst.CompareTo(l.dst));
            pq.insert((S - 1, 0, 0));
            while (0 < pq.num)
            {
                var v = pq.extract();
                var u = v.node;
                var p = v.p;
                var np = (p + 1) % 3;
                if (dp[u][p] < v.dst) continue;
                foreach (var to in graph[u])
                {
                    if (dp[to][np] != MAX) continue;
                    if (dp[u][p] + 1 < dp[to][np])
                    {
                        dp[to][np] = dp[u][p] + 1;
                        pq.insert((to, dp[to][np], np));
                    }
                }
            }
            if (MAX == dp[T - 1][0])
                WriteLine(-1);
            else
                WriteLine(dp[T - 1][0] / 3 );
        }
        class priority_queue<T>
        {
            Comparison<T> cmp;
            T[] A = new T[2000000];
            // var pq = new priority_queue<long>((l, r) => l.CompareTo(r));
            // とすれば値の大きいものから優先
            public priority_queue(Comparison<T> c)
            {
                cmp = c;
            }
            public int num { get; private set; } = 0;
            public void insert(T v)
            {
                A[num] = v;
                ++num;
                increaseKey(num);
            }
            void increaseKey(int i)
            {
                while (1 < i && 0 < cmp(A[i - 1], A[i / 2 - 1]))
                {
                    var t = A[i - 1];
                    A[i - 1] = A[i / 2 - 1];
                    A[i / 2 - 1] = t;
                    i = i / 2;
                }
            }
            public T extract()
            {
                if (num < 1) return default(T);
                var max = A[0];
                A[0] = A[num - 1];
                --num;
                maxHeapify(1);
                return max;
            }
            void maxHeapify(int i)
            {
                var H = num;
                var l = left(i);
                var r = right(i);
                var largest = 0;
                if (l <= H && 0 < cmp(A[l - 1], A[i - 1])) { largest = l; } else { largest = i; }
                if (r <= H && 0 < cmp(A[r - 1], A[largest - 1])) { largest = r; }
                if (largest != i)
                {
                    var t = A[i - 1];
                    A[i - 1] = A[largest - 1];
                    A[largest - 1] = t;
                    maxHeapify(largest);
                }
            }
            int left(int l) => l * 2;
            int right(int r) => r * 2 + 1;
        }
        static long Gcd(long x, long y) => y == 0 ? x : Gcd(y, x % y);
        static long Lcm(long x, long y) => x / Gcd(x, y) * y;

        static long Mod = (long)1e9 + 7;
        static public long[] Sarray() { return ReadLine().Split().Select(long.Parse).ToArray(); }
        static public List<long> Slist() { return ReadLine().Split().Select(long.Parse).ToList(); }
        static public (T1 a, T2 b) Slice<T1, T2>()
        {
            var t = ReadLine().Split();
            return (
            (T1)Convert.ChangeType(t[0], typeof(T1)),
            (T2)Convert.ChangeType(t[1], typeof(T2)));
        }
        static public (T a, T b) Slice2<T>()
        {
            var t = ReadLine().Split();
            return (
            (T)Convert.ChangeType(t[0], typeof(T)),
            (T)Convert.ChangeType(t[1], typeof(T)));
        }
        static public (T a, T b, T c) Slice3<T>()
        {
            var t = ReadLine().Split();
            return (
            (T)Convert.ChangeType(t[0], typeof(T)),
            (T)Convert.ChangeType(t[1], typeof(T)),
            (T)Convert.ChangeType(t[2], typeof(T)));
        }
        static public (T a, T b, T c, T d) Slice4<T>()
        {
            var t = ReadLine().Split();
            return (
            (T)Convert.ChangeType(t[0], typeof(T)),
            (T)Convert.ChangeType(t[1], typeof(T)),
            (T)Convert.ChangeType(t[2], typeof(T)),
            (T)Convert.ChangeType(t[3], typeof(T)));
        }
    }
}