import sys

def input(): return sys.stdin.readline().strip()



def resolve():

    n=int(eval(input()))

    edge = [[] * n for i in range(n)] # rangeに頂点の数。繋がっている頂点を投入するリスト

    for i in range(n): # rangeに辺の数

        x, y = i,int(eval(input()))

        y = y - 1

        edge[x].append(y)

    sagasu=0

    cnt=0

    while True:

        a=edge[sagasu]

        if a==[1]:

            print((cnt+1))

            break

        else:

            if cnt>n:

                print((-1))

                break

            else:

                cnt+=1

                sagasu=a[0]

resolve()
import sys

def input(): return sys.stdin.readline().strip()



def resolve():

    n=int(eval(input()))

    l=[int(eval(input())) for i in range(n)]

    light=0

    cnt=0

    while True:

        if l[light]==2:

            cnt+=1

            print(cnt)

            break

        else:

            if cnt>n:

                print((-1))

                break

            else:

                light=l[light]-1

                cnt+=1

            

resolve()

import sys


def input():
    return sys.stdin.readline().strip()


def resolve():
    n = int(eval(input()))
    edge = [[] * n for i in range(n)]  # rangeに頂点の数。繋がっている頂点を投入するリスト
    for i in range(n):  # rangeに辺の数
        x, y = i, int(eval(input()))
        y = y - 1
        edge[x].append(y)
    sagasu = 0
    cnt = 0
    while True:
        a = edge[sagasu]
        if a == [1]:
            print((cnt + 1))
            break
        else:
            if cnt > n:
                print((-1))
                break
            else:
                cnt += 1
                sagasu = a[0]


resolve()

import sys


def input():
    return sys.stdin.readline().strip()


def resolve():
    n = int(eval(input()))
    l = [int(eval(input())) for i in range(n)]
    light = 0
    cnt = 0
    while True:
        if l[light] == 2:
            cnt += 1
            print(cnt)
            break
        else:
            if cnt > n:
                print((-1))
                break
            else:
                light = l[light] - 1
                cnt += 1


resolve()

-    edge = [[] * n for i in range(n)]  # rangeに頂点の数。繋がっている頂点を投入するリスト -    for i in range(n):  # rangeに辺の数 -        x, y = i, int(eval(input())) -        y = y - 1 -        edge[x].append(y) -    sagasu = 0 +    l = [int(eval(input())) for i in range(n)] +    light = 0 -        a = edge[sagasu] -        if a == [1]: -            print((cnt + 1)) +        if l[light] == 2: +            cnt += 1 +            print(cnt) +                light = l[light] - 1 -                sagasu = a[0]