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.IO;
using System.Collections.Generic;
namespace I_am_the_green_coder{
    class Inonoa{
        // なんか入力用の関数作ったけどまだテストしてないので使う前に確かめて
        static void Main(){
            long[] nm = ILongs();
            long n = nm[0];
            long m = nm[1];

            long[][] uv = ILongLongs(m);
            long[] st = ILongs();
            long s = st[0];
            long t = st[1];

            List<long>[] ikisaki = new List<long>[n];
            for(int i=0;i<n;i++){
                ikisaki[i] = new List<long>();
            }
            for(int i=0;i<m;i++){
                ikisaki[uv[i][0]-1].Add(uv[i][1]-1);
            }

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

            HashSet<long>[] ikisaki2 = new HashSet<long>[n];
            for(int i=0;i<n;i++){
                ikisaki2[i] = new HashSet<long>();
            }
            

            for(int i=0;i<n;i++){
                /*
                Queue<long[]> q = new Queue<long[]>();
                q.Enqueue(new long[2]{i,0});
                while(q.Count>0){
                    long[] ima = q.Dequeue();
                    foreach(long saki in ikisaki[ima[0]]){
                        if(ima[1]==2){
                            newikisaki[i].Add(saki);
                        }else{
                            q.Enqueue(new long[2]{saki,ima[1]+1});
                        }
                    }
                }
                */
                foreach(long saki in ikisaki[i]){
                    foreach(long goal in ikisaki[saki]){
                        ikisaki2[i].Add(goal);
                    }
                }
            }
            for(int i=0;i<n;i++){
                foreach(long a in ikisaki[i]){
                    foreach(long b in ikisaki2[a]){
                        newikisaki[i].Add(b);
                    }
                }
            }

            Queue<long[]> newq = new Queue<long[]>();
            newq.Enqueue(new long[2]{s-1,0});
            bool[] moumita = new bool[n];
            moumita[s-1] = true;

            while(newq.Count>0){
                long[] ima = newq.Dequeue();
                foreach(long saki in newikisaki[ima[0]]){
                    if(saki==t-1){
                        Console.WriteLine(ima[1]+1);
                        return;
                    }else if(!moumita[saki]){
                        newq.Enqueue(new long[2]{saki,ima[1]+1});
                        moumita[saki] = true;
                    }
                }
            }
            Console.WriteLine(-1);
        }

        static readonly long MOD = 1000000007;

        ///<summary>a_1, a_2, a_3, ...みたいなの</summary>
        static long[] ILongs(){
            string[] s = Console.ReadLine().Split();
            long[] re = new long[s.Length];
            for(long i=0;i<s.Length;i++){
                re[i] = long.Parse(s[i]);
            }
            return re;
        }

        ///<summary>xooxoxoxxoxo...みたいなの</summary>
        static char[] IChars(){
            return Console.ReadLine().ToCharArray();
        }

        ///<summary>n m とかをちょっとはやくかくだけ</summary>
        static string[] IStrings(){
            return Console.ReadLine().Split();
        }

        ///<summary>n行の入力を取ったり</summary>
        static long[][] ILongLongs(long n){
            long[][] re = new long[n][];
            for(long i=0;i<n;i++){
                string[] s = Console.ReadLine().Split();
                re[i] = new long[s.Length];
                for(long j=0;j<s.Length;j++){
                    re[i][j] = long.Parse(s[j]);
                }
            }
            return re;
        }

        static char[][] ICharsChars(long n){
            char[][] re = new char[n][];
            for(long i=0;i<n;i++){
                re[i] = Console.ReadLine().ToCharArray();
            }
            return re;
        }
    }
}