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# (Mono 4.6.2.0) (judge verdict: Time Limit Exceeded)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static System.Console;
using static System.Math;

namespace AtTest.ABC_132
{
    class E
    {
        static void Main(string[] args)
        {
            Method(args);
        }

        static void Method(string[] args)
        {
            int[] nm = ReadInts();
            int n = nm[0];
            int m = nm[1];
            List<int>[] baseGraph = new List<int>[n];
            for(int i = 0; i < n; i++)
            {
                baseGraph[i] = new List<int>();
            }

            for(int i = 0; i < m; i++)
            {
                int[] uv = ReadInts();
                int u = uv[0]-1;
                int v = uv[1]-1;
                baseGraph[u].Add(v);
            }
            int[] st = ReadInts();
            int s = st[0]-1;
            int t = st[1]-1;

            List<int>[] graph = new List<int>[n];
            for(int i = 0; i < n; i++)
            {
                graph[i] = new List<int>();
            }

            HashSet<int>[,] has = new HashSet<int>[n, 4];
            for(int i = 0; i < n; i++)
            {
                has[i, 0] = new HashSet<int>();
                has[i, 0].Add(i);
                has[i, 1] = new HashSet<int>();
                has[i, 2] = new HashSet<int>();
                has[i, 3] = new HashSet<int>();
            }
            for(int i = 0; i < 3; i++)
            {
                for(int j = 0; j < n; j++)
                {
                    for(int k = 0; k < baseGraph[j].Count; k++)
                    {
                        foreach(int key in has[j, i])
                        {
                            has[baseGraph[j][k], i + 1].Add(key);
                        }
                    }
                }
            }

            for(int i = 0; i < n; i++)
            {
                foreach(int key in has[i, 3])
                {
                    graph[key].Add(i);
                }
            }

            Queue<int[]> queue = new Queue<int[]>();
            queue.Enqueue(new int[2] { s, 0 });
            int[] dists = new int[n];
            for (int i = 0; i < n; i++) dists[i] = int.MaxValue;
            while (queue.Count > 0)
            {
                int[] val = queue.Dequeue();
                if (dists[val[0]] < val[1]) continue;

                dists[val[0]]= val[1];
                if (val[0] == t) break;
                for(int i = 0; i < graph[val[0]].Count; i++)
                {
                    if (dists[graph[val[0]][i]] < val[1] + 1) continue;

                    queue.Enqueue(new int[2] { graph[val[0]][i], val[1] + 1 });
                }
            }

            if (dists[t] == int.MaxValue) WriteLine(-1);
            else WriteLine(dists[t]);
        }

        private static string Read() { return ReadLine(); }
        private static char[] ReadChars() { return Array.ConvertAll(Read().Split(), a => a[0]); }
        private static int ReadInt() { return int.Parse(Read()); }
        private static long ReadLong() { return long.Parse(Read()); }
        private static double ReadDouble() { return double.Parse(Read()); }
        private static int[] ReadInts() { return Array.ConvertAll(Read().Split(), int.Parse); }
        private static long[] ReadLongs() { return Array.ConvertAll(Read().Split(), long.Parse); }
        private static double[] ReadDoubles() { return Array.ConvertAll(Read().Split(), double.Parse); }
    }
}