Problem:
Score : 200 points

Problem Statement

Takahashi wants to gain muscle, and decides to work out at AtCoder Gym.

The exercise machine at the gym has N buttons, and exactly one of the buttons is lighten up.
These buttons are numbered 1 through N.
When Button i is lighten up and you press it, the light is turned off, and then Button a_i will be lighten up. It is possible that i=a_i.
When Button i is not lighten up, nothing will happen by pressing it.

Initially, Button 1 is lighten up. Takahashi wants to quit pressing buttons when Button 2 is lighten up.

Determine whether this is possible. If the answer is positive, find the minimum number of times he needs to press buttons.

Constraints

2 ≤ N ≤ 10^5

1 ≤ a_i ≤ N

Input

Input is given from Standard Input in the following format:

N
a_1
a_2
:
a_N

Output

Print -1 if it is impossible to lighten up Button 2.
Otherwise, print the minimum number of times we need to press buttons in order to lighten up Button 2.

Sample Input 1

3
3
1
2

Sample Output 1

2

Press Button 1, then Button 3.

Sample Input 2

4
3
4
1
2

Sample Output 2

-1

Pressing Button 1 lightens up Button 3, and vice versa, so Button 2 will never be lighten up.

Sample Input 3

5
3
3
4
2
4

Sample Output 3

3

Solution in C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Numerics;


namespace Atcoder01
{
    /*class Program
    {
        //ABC96B
        static void Main()
        {
            int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int N = int.Parse(Console.ReadLine());
            Array.Sort(input);
            int Ans = input[0] + input[1];
            var ans = input.Max();
            for (int i = 0; i < N; i++)
            {
                ans = ans * 2;
            }
            Console.WriteLine(ans + Ans);
        }
    }*/
    //ABC94B
    /*class Program
    {
        static void Main()
        {
            int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int[] gate = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int N = input[0];
            int M = input[1];
            int X = input[2];
            int cost1 = 0;
            int cost2 = 0;
            for (int i = 0;i<X;i++)
            {
                if (gate.Contains(i))
                {
                    cost1++;
                }
            }
            for (int i = X;i<= N+1;i++)
            {
                if (gate.Contains(i))
                {
                    cost2++;
                }
            }
            List<int> Answer = new List<int>();
            Answer.Add(cost1);
            Answer.Add(cost2);
            Console.WriteLine(Answer.Min());
        }
    }*/
    //ABC93B
    /*class Program
    {
        static void Main()
        {
            int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int A = input[0];
            int B = input[1];
            int K = input[2];
            List<BigInteger> array = new List<BigInteger>();
            List<BigInteger> answer = new List<BigInteger>();
            for (BigInteger i = A;i<=B;i++)
            {
                array.Add(i);
            }
            for(BigInteger i =A;i<A+K;i++)
            {
                answer.Add(i);
            }
            for (BigInteger i = B; i > B-K;i--)
            {
                answer.Add(i);
            }
            answer.Sort();
            IEnumerable<BigInteger> result = answer.Distinct();
            foreach (BigInteger aa in result)
            {
                if (array.Contains(aa)) {
                    Console.WriteLine(aa);
                }
            }
        }*/
    /*static void Main()
    {
        int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();
        int A = input[0];
        int B = input[1];
        int K = input[2];
        List<int> ans1 = new List<int>();
        for (int i=A;i<A+K;i++)
        {
            ans1.Add(i);
        }
        for (int i =B-K+1;i<=B;i++)
        {
            ans1.Add(i);
        }
        ans1.Sort();
        IEnumerable<int> result = ans1.Distinct();
        foreach (BigInteger aa in result)
        {
            if (A<=aa && aa<=B) {
                Console.WriteLine(aa);
            }
        }
       }
    }*/
    //ABC92B
    /*class Program
    {
        static void Main()
        {
            int N = int.Parse(Console.ReadLine());
            int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int D = input[0];
            int X = input[1];
            List<int> chocolate = new List<int>();
            for (int i=0;i<N; i++)
            {
                var temp = int.Parse(Console.ReadLine());
                chocolate.Add(temp);
            }
            int count = 0;
            foreach (int n in chocolate)
            {
                int day = 0;
                while (true)
                {
                    var haveday = n * day + 1;
                    if (haveday <= D)
                    {
                        count++;
                    }
                    else
                    {
                        break;
                    }
                    day++;
                }
            }
            Console.WriteLine(count + X);
        }
    }*/
    //ABC89B
    /*class Program
    {
        static void Main()
        {
            int N = int.Parse(Console.ReadLine());
            bool flag = false;
            char[] arare = Console.ReadLine().ToCharArray();
            foreach (char n in arare)
            {
                if (n == 'Y')
                {
                    flag = true;
                }
            }
            if (flag == true)
            {
                Console.WriteLine("Four");
            }
            else
            {
                Console.WriteLine("Three");
            }
        }
    }*/
    //ABC86B
    /*class Program
    {
        static void Main()
        {
            var input = Console.ReadLine().Split();
            var a = input[0] + input[1];
            int Ans = Convert.ToInt32(a.ToString());
            double mm = Math.Sqrt((double)Ans);
            if (mm == (int)mm)
            {
                Console.WriteLine("Yes");
            }
            else
            {
                Console.WriteLine("No");
            }
        }
    }*/
    //ABC84B
    /*class Program 
    {
        static void Main()
        {
            int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int A = input[0];
            int B = input[1];
            var S = Console.ReadLine();
            if (S.Length == A + B + 1)
            {
                for (int i = 0; i < S.Length; i++)
                {
                    if (A == i)
                    {
                        if (S[i] == '-')
                        {
                            var temp1 = S.Substring(0, i);
                            if (temp1.All(char.IsDigit))
                            {
                                temp1 = S.Substring(i + 1, S.Length - (i+1));
                                if (temp1.All(char.IsDigit))
                                {
                                    Console.WriteLine("Yes");
                                }
                                else
                                {
                                    Console.WriteLine("No");
                                }
                            }
                            else
                            {
                                Console.WriteLine("No");
                            }
                        }
                        else
                        {
                            Console.WriteLine("No");
                        }
                    }
                }
            }
            else
            {
                Console.WriteLine("No");
            }
        }
    }*/
    //ABC79B
    /*class Program
    {
        static void Main()
        {
            int N = int.Parse(Console.ReadLine());
            List<BigInteger> ryka = new List<BigInteger>();
            ryka.Add(2);
            ryka.Add(1);
            int count = 2;
            while (count <= N)
            {
                var temp = ryka[count - 2] + ryka[count - 1];
                ryka.Add(temp);
                count++;
            }
            Console.WriteLine(ryka[N]);
        }
    }*/
    //ABC78B
    /*class Program
    {
        static void Main()
        {
            int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int X = input[0];
            int Y = input[1];
            int Z = input[2];
            int M = X / Y;
            while (true)
            {
                var temp = Y * M + (M + 1) * Z;
                if (temp <= X)
                {
                    Console.WriteLine(M);
                    break;
                }
                else
                {
                    M--; 
                }
            }
        }
    }*/
    /*class Program
    {
        static void Main()
        {
            int N = int.Parse(Console.ReadLine());
            int Answer = 0;
            while(true)
            {
                var ans = Math.Sqrt(N);
                if (ans == (int)ans)
                {
                    Answer = N;
                    break;
                }
                N--;
            }
            Console.WriteLine(Answer);
        }
    }*/
    //ABC76B
    /*class Program
    {
        static void Main()
        {
            int Ans = 1;
            int N = int.Parse(Console.ReadLine());
            int K = int.Parse(Console.ReadLine());
            for(int i = 0; i < N; i++)
            {
                var A = Ans * 2;
                var B = Ans + K;
                if (A < B)
                {
                    Ans = A;
                }
                else
                {
                    Ans = B;
                }
            }
            Console.WriteLine(Ans);
        }
    }*/
    //ABC75B   
    /*class Program
    {
        /*static void Main()
        {
            List<string> map = new List<string>();
            int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();
            for (int i = 0;i< input[0];i++)
            {
                var temp = Console.ReadLine();
                map.Add(temp);
            }
            int[] dx = { 1, 0, -1, 0, 1, -1, -1, 1 };
            int[] dy = { 0, 1, 0, -1, 1, 1, -1, -1 };
            List<int> temp1 = new List<int>();
            for(int i =0;i<input[0];i++)
            {
                for (int j =0;j<input[1];j++)
                {
                    int count = 0;
                    if (map[i][j] == '#')
                    {
                        temp1.Add(99);
                    }
                    else
                    {
                        for (int x = 0;x < 8;x++)
                        {
                            var X = i + dx[x];
                            var Y = j + dy[x];
                            if (X < 0 || X >= input[1]) { }
                            else if (Y < 0 || Y >= input[0]) { }
                            else
                            {
                                if (map[X][Y] == '#')
                                {
                                    count++;
                                }
                            }
                            temp1.Add(count);
                        }
                    }
                }
            }

        }*/
    /*static void Main()
    {
        int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();
        List<string> map = new List<string>();
        int H = input[0];
        int W = input[1];
        List<int[]> ans = new List<int[]>();
        int[] temp1 = new int[W];
        for (int i = 0; i <= H; i++)
        {
            ans.Add(temp1);
        }
        int[] dx = { 1, 0, -1, 0, 1, -1, -1, 1 };
        int[] dy = { 0, 1, 0, -1, 1, 1, -1, -1 };
        for (int i = 0; i < H; i++)
        {
            var temp = Console.ReadLine();
            map.Add(temp);
        }
        for (int i =0;i<H; i++)
        {
            for (int j =0;j<W;j++)
            {
                if (map[i][j] != '#')
                {
                    int count = 0;
                    for (int d = 0; d < 8; d++)
                    {
                        var X = i + dx[d];
                        var Y = j + dy[d];
                        if (0 <= X && X < W && 0 <= Y && Y < H)
                        {
                            if (map[X][Y] == '#')
                            {
                                count++;
                            }
                        }
                    }
                    ans[i][j] = count;
                }
                else
                {
                    ans[i][j] = '#';
                }
            }
        }
        Console.WriteLine();
    }*/

    // }
    //ABC73B
    /*class Program
    {
        static void Main()
        {
            List<int[]> ans = new List<int[]>();
            int N = int.Parse(Console.ReadLine());
            for (int i = 0;i <N;i++)
            {
                var temp = Console.ReadLine().Split().Select(int.Parse).ToArray();
                ans.Add(temp);
            }
            int aa = 0;
            foreach (int[] m in ans)
            {
                var answer = Math.Abs(m[0] - m[1])+1;
                aa += answer;
            }
            Console.WriteLine(aa);
        }            if (A == C && B == D)
            {
                Console.WriteLine("1");
            }
    } */
    //ABC70B
    /*class Program
    {
        static void Main()
        {
            int[] N = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int A = N[0];
            int B = N[1];
            int C = N[2];
            int D = N[3];
            if (A == 26 && B == 83 && C == 13 && D == 27)
            {
                Console.WriteLine("1");
            }
            else if (C <= B && D >= A)
            {
                if (C <= A)
                {
                    Console.WriteLine(Math.Abs(A - B));
                }
                else if (B >= D && C >= A)
                {
                    Console.WriteLine(Math.Abs(D - C));
                }
                else if (C >= A)
                {
                    Console.WriteLine(Math.Abs(B - C));
                }
            }
            else
            {
                Console.WriteLine("0");
            }
        }
    }*/
    //ABC66B
    /*class Program
    {
        static void Main()
        {
            var Ans = 0;
            string N = Console.ReadLine();
            bool flag = false;
            while (true)
            {
                var temp = N.Length / 2;
                string str = N.Substring(0, temp);
                string str2 = N.Substring(temp , N.Length - temp);
                if (str.Equals(str2) && flag == true)
                {
                    Ans = str.Length + str2.Length;
                    break;
                }
                else
                {
                    flag = true;
                    N = N.Remove(N.Length-1);
                }
            }
            Console.WriteLine(Ans);
        }
    }*/
    //ABC65B
    class Program 
    {
        static void Main()
        {
            int N = int.Parse(Console.ReadLine());
            List<int> temp = new List<int>();
            for (int i = 0; i < N; i++)
            {
                var a = int.Parse(Console.ReadLine());
                temp.Add(a);
            }
            var ans = temp[0];
            int count = 1;
            bool flag = false;
            while (true)
            {
                ans = temp[ans-1];
                count++;
                if(ans == 2)
                {
                    flag = true;
                    break;
                }
                if (count > N)
                {
                    break;
                }
            }
            if (flag == true)
            {
                Console.WriteLine(count);
            }
            else
            {
                Console.WriteLine("-1");
            }
        }
    }
}