CodeEff/ECCO huggingface dataset:
# Original solution
import sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**20
def I(): return int(eval(input()))
def F(): return float(eval(input()))
def S(): return eval(input())
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()

def resolve():
    N = I()
    a = [I() - 1 for _ in range(N)]

    c = 0
    ans = 0
    is_ok = True
    while True:
        c = a[c]
        ans += 1
        if c == 0:
            is_ok = False
            break
        if c == 1:
            break

    if is_ok:
        print(ans)
    else:
        print((-1))

if __name__ == '__main__':
    resolve()

# Optimized solution
import sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**20
def I(): return int(eval(input()))
def F(): return float(eval(input()))
def S(): return eval(input())
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()

def resolve():
    N = I()
    a = [I() - 1 for _ in range(N)]

    c = 0
    ans = 0
    is_ok = True
    while True:
        c = a[c]
        ans += 1
        if c == 0 or ans > N:
            is_ok = False
            break
        if c == 1:
            break

    if is_ok:
        print(ans)
    else:
        print((-1))

if __name__ == '__main__':
    resolve()
# Original solution
N = int(eval(input()))
a = [int(eval(input())) for i in range(N)]
isFound = False
ai = 1
for i in range(1, N + 1):
    ai = a[ai - 1]
#    print(f'a{i} = {ai}')
    if (ai == 2):
        isFound = True
        print(i)
        break
if (isFound is False):
    print((-1))


# Optimized solution
import sys

N = list(map(int, sys.stdin))
bot = N[1]

for i in range(N[0]):
    if bot == 2:
        print((i+1))
        break
    bot = N[bot]
else:
    print((-1))
# Original solution
def main():
  n = int(eval(input()))
  a = [int(eval(input())) for i in range(n)]
  check_id=1
  check_list=[]
  while True:
    if check_id in check_list:
      print((-1))
      break
    if check_id==2:
      print((len(check_list)))
      break      
    check_list.append(check_id)
    check_id=a[check_id-1]  
      
main()

# Optimized solution
def main():
  n = int(eval(input()))
  a = [int(eval(input())) for i in range(n)]
  check_id=1
  for i in range(1,n+1):
    check_id=a[check_id-1]
    if check_id==2:
      print(i)
      break
  if check_id!=2:
      print((-1))
main()
# Original solution
import sys

def main():
    input = sys.stdin.readline
    N = int(eval(input()))
    btn = [int(eval(input()))-1 for _ in range(N)]
    i = 0
    li = [0]
    #print(btn)
    
    for _ in range(N):
        k = btn[i]
        #print(k, li)
        if k in li:
            print('-1')
            break
        else:
            if k != 1:
                li.append(k)
            else:
                print((len(li)))
                break
        i = k
    
if __name__ == '__main__':
    main()

# Optimized solution
import sys

def main():
    input = sys.stdin.readline
    N = int(eval(input()))
    btn = [int(eval(input()))-1 for _ in range(N)]
    i = 0
    li = [False]*N
    #print(btn)
    
    for _ in range(N):
        li[i] = True
        k = btn[i]
        
        #print(k, li)
        if li[k]:
            print('-1')
            break
        elif k == 1:
            print((sum(li)))
            break
        
        i = k
    
if __name__ == '__main__':
    main()
# Original solution
n = int(eval(input()))
lis = [1]
cou = 0
ans = 0
bo = []
for i in range(n):
    bo.append(int(eval(input())))
for i in range(n):
    a = bo[cou]
    if a not in lis:
        lis.append(a)
    else:
        print("-1")
        break
    cou = a-1
    ans += 1
    if a == 2:
        print(ans)
        break

# Optimized solution
n = int(eval(input()))
lis = []
p = 1
s = -1
for i in range(n):lis.append(int(eval(input())))
for m in range(n):
    p = lis[p-1]
    if p == 2:
        s = m + 1
        break
print(s)
# Original solution
N = int(eval(input()))
A = []
for i in range(N):
    A.append(int(eval(input()))-1)  
    
index = 0
count = 0
flag = True
check = []
while flag:
    index = A[index]
    check.append(index)
    count += 1
    if len(list(set(check))) != len(check):
        flag = False
        count = -1
    if index == 1:
        flag = False
print(count)

# Optimized solution
N = int(eval(input()))
A = []
for i in range(N):
    A.append(int(eval(input()))-1)  
    
index = 0
count = 1
flag = True
while flag:
    index = A[index]
    if index == 1:
        flag = False
    else:
        count += 1
    if count > N:
        flag = False
        count = -1
print(count)
# Original solution
N = int(eval(input()))
A = [int(eval(input())) for _ in range(N)]

c = 0
n = 1
while True:
    n = A[n-1]
    c += 1
    if n == 1:
        ok = False
        break
    if n == 2:
        ok = True
        break
if ok:
    print(c)
else:
    print((-1))

# Optimized solution
N = int(eval(input()))
A = [int(eval(input())) for _ in range(N)]

c = 0
n = 1
checked = [False for _ in range(N)]
checked[0] = True
for _ in range(N+1):
    n = A[n-1]
    c += 1
    if n == 2:
        ok = True
        break
    if checked[n-1]:
        ok = False
        break
    checked[n-1] = True
if ok:
    print(c)
else:
    print((-1))
# Original solution
N = int(eval(input()))
A = [int(eval(input())) for _ in range(N)]
current = 0
ans = 0

while True:
    current = A[current] - 1
    ans += 1
    if current == 0:
        print((-1))
        break

    if current == 1:
        print(ans)
        break

# Optimized solution
N = int(eval(input()))
A = [int(eval(input())) for _ in range(N)]
current = 0
visited = {0}
ans = 0

while True:
    current = A[current] - 1
    ans += 1

    if current in visited:
        print((-1))
        break

    if current == 1:
        print(ans)
        break

    visited.add(current)
# Original solution
n = int(eval(input()))
a = [int(eval(input())) for _ in range(n)]

ans = 0
visited = set([0])
next_visit_idx = 0
while True:
  next_visit_idx = a[next_visit_idx] -1
  ans += 1
  if next_visit_idx == 1:
    print(ans)
    break
  if next_visit_idx in visited:
    print((-1))
    break

# Optimized solution
n = int(eval(input()))
a = [int(eval(input())) for _ in range(n)]

ans = 0
visited = set([0])
next_visit_idx = 0
while True:
  next_visit_idx = a[next_visit_idx] -1
  ans += 1
  if next_visit_idx == 1:
    print(ans)
    break
  if next_visit_idx in visited:
    print((-1))
    break
    
  visited.add(next_visit_idx)
# Original solution
#coding: utf-8

N = int(eval(input()))
a = []
for i in range(N):
    a.append(int(eval(input())))

start = 0
count = 0

for i in range(N):
    count += 1
    next = a[start]
    if(next == 2):
        print(count)
        break
    start = next - 1
else:
    print((-1))

# Optimized solution
import sys

N = list(map(int, sys.stdin))
bot = N[1]

for i in range(N[0]):
    if bot == 2:
        print((i+1))
        break
    bot = N[bot]
else:
    print((-1))
# Original solution
class UFT(object):
  def __init__(self,N):
    self.par={i:i for i in range(1,N+1)}
    self.h={i:0 for i in range(1,N+1)}
  def find(self,n):
    if self.par[n]==n:
      return n
    else:
      self.par[n]=self.find(self.par[n])
      return self.par[n]
  def unite(self,a,b):
    a=self.find(a)
    b=self.find(b)
    if self.h[a]>self.h[b]:
      self.par[b]=a
    else:
      self.par[a]=b
      if self.h[a]==self.h[b]:
        self.h[b]+=1
  def issame(self,a,b):
    return self.find(a)==self.find(b)

N=int(eval(input()))
obj=UFT(N)
d={i:0 for i in range(1,N+1)}
for i in range(N):
  b=int(eval(input()))
  d[i+1]=b
  obj.unite(i+1,b)

if obj.issame(2,1):
  c=1
  e=d[1]
  while e!=2:
    c+=1
    e=d[e]
  print(c)
else:
  print("-1")

# Optimized solution
n=int(eval(input()))
d={i:int(eval(input())) for i in range(1,n+1)}

f=True
s=1
for i in range(n):
  if s==2:
    print(i)
    f=False
    break
  s=d[s]
if f:
  print((-1))
# Original solution
N = int(eval(input()))
buttons = [int(eval(input())) for i in range(N)]
visited = [1]

cur = 1
count = 0
for i in range(N):
    cur = buttons[cur-1]
    count += 1

    if cur in visited:
        print((-1))
        exit()

    visited.append(cur)

    if cur == 2:
        print(count)
        exit()

print((-1))

# Optimized solution
N = int(eval(input()))
buttons = [int(eval(input())) for i in range(N)]

cur = 1
for i in range(N):
    cur = buttons[cur-1]

    if cur == 2:
        print((i+1))
        exit()

print((-1))
# Original solution
N=int(eval(input()))
a=[int(eval(input())) for i in range(N)]
ans=0
b=1
while(1):
    ans+=1
    b=a[b-1]
    if b==1:
        ans=-1
        break
    if b==2:break
print(ans)

# Optimized solution
N=int(eval(input()))
a=[int(eval(input())) for i in range(N)]
j=[0]*N
ans=0
b=1
while(1):
    ans+=1
    b=a[b-1]
    j[b-1]+=1
    if j[b-1]==2:
        ans=-1
        break
    if b==2:break
print(ans)
# Original solution
n = int(eval(input()))
a = [0] * n
for i in range(n):
    a[i] = int(eval(input()))

counter = 0
now = 1
visit = [False] * n
visit[0] = True
while True:
    counter += 1
    if a[now - 1] == 2:
        print(counter)
        break
    elif visit[a[now - 1]-1] == True:
        print((-1))
        break
    else:
        visit[a[now - 1]-1] = True
    now = a[now - 1]


# Optimized solution
n = int(eval(input()))
a = [0] * n
for i in range(n):
    a[i] = int(eval(input()))

visit = set()
now = 1
for i in range(1, n + 10):
    if now not in visit:
        visit.add(now)
        now = a[now - 1]
    else:
        print((-1))
        break
    if now == 2:
        print(i)
        break
# Original solution
N = int(eval(input()))

dic = {}
for n in range(1,N+1):
  dic[n] = int(eval(input()))
  
lis = []
now = 1
count = 0
flag = False
while True:
  #count += 1
  if now in lis:
    break
  if dic[now] == 2:
    flag = True
    break
  lis.append(now)
  now = dic[now]

if flag:
  print((len(lis)+1))
else:
  print((-1))

# Optimized solution
N = int(eval(input()))

dic = {}
for n in range(1,N+1):
  dic[n] = int(eval(input()))

now = 1
count = 0
flag = False
while True:
  if now==2:
    flag = True
    break
  if now not in dic:
    break
  now = dic.pop(now)
  #del dic[now]
  count += 1

if flag:
  print(count)
else:
  print((-1))
# Original solution
n = int(eval(input()))
li = [int(eval(input())) for _ in range(n)]

cnt = 0
liuse = [1]
place = 1
for i in range(n):
  place = li[place-1]
  liuse.append(place)
  cnt += 1
  if place == 2:
    print(cnt)
    exit()
  if len(liuse) > len(set(liuse)):
    print((-1))
    exit()

# Optimized solution
n = int(eval(input()))
li = [int(eval(input())) for _ in range(n)]

liuse = [0]*n
liuse[0] = 1
for i in range(1,n):
  liuse[i] = li[liuse[i-1]-1]
if 2 in liuse:
  print((liuse.index(2)))
else:
  print((-1))
# Original solution
n=int(eval(input()))
a=[]
b=[]
for i in range(n):
    a.append(int(eval(input())))
next=0
count=0
while next!=1:
    if a[next]-1 in b:
        count=-1
        break
    b.append(a[next]-1)
    next=a[next]-1
    count+=1
print(count)
    


# Optimized solution
n=int(eval(input()))
a = [int(eval(input())) for _ in range(n)]
b=[]
next=0
count=0
for i in range(n):
    next=a[next]-1
    count+=1
    if next==1:
        print(count)
        exit()
print((-1))
# Original solution
n=int(eval(input()))
a=[]
for i in range(n):
  a.append(int(eval(input())))
i=0
count=1
while(1):
  if a[i]==1:
    frag=-1
    break
  elif a[i]==2:
    frag=count
    break
  else:
    i=a[i]-1
    count+=1
print(frag)  

# Optimized solution
n=int(eval(input()))
a=[]
for i in range(n):
  a.append(int(eval(input())))
i=0
count=1
frag=-1
for j in range(n):
  if a[i]==2:
    frag=count
    break
  else:
    i=a[i]-1
    count+=1
print(frag)
# Original solution
n = int(eval(input()))
A = [int(eval(input())) for _ in range(n)]
sec = []
count, cur = 0, 1
while cur not in sec:
    sec.append(cur)
    if cur == 2:
        break
    cur = A[cur - 1]
    count += 1
print((count if 2 in sec else -1))

# Optimized solution
n = int(eval(input()))
A = [int(eval(input())) for _ in range(n)]
count, current = 0, 1
while current != 2:
    current = A[current - 1]
    count += 1
    if count > n:
        count = -1
        break
print(count)
# Original solution
# ABC 65 B

def resolve():
    N = int(eval(input()))

    a = [0] * (N+1)
    for i in range(N):
        a[i+1] = int(eval(input()))

    b = 1
    ans = 0
    while (True):
        tmp = a[b]
        a[b] = 0
        b = tmp
        ans += 1

        if b == 2:
            print(ans)
            return
        elif b == 0:
            print((-1))
            return

    print((-1))
    return

if __name__ == "__main__":
    resolve()


# Optimized solution
def resolve():
  n = int(eval(input()))
  a = []
  for i in range(n):
    a.append(int(eval(input())) - 1)

  ans = 0
  idx = 0
  while True:
    if a[idx] == -1:
      print("-1")
      return
    if idx == 1:
      break
    ans += 1
    tmp = a[idx]
    a[idx] = -1
    idx = tmp
  print(ans)
  return

if __name__ == "__main__":
  resolve()
# Original solution
n = int(eval(input()))
a = [int(eval(input())) for i in range(n)]

ans = 0
i = 0
while n > 0:
    ans += 1
    if a[i] == 2:
        print(ans)
        quit()
    else:
        i = a[i]-1
    n -= 1
print((-1))


# Optimized solution
n = int(eval(input()))
a = [int(eval(input())) for i in range(n)]

ans = -1
next_a = a[0]

for i in range(1, n):
    if next_a == 2:
        ans = i
        print(ans)
        quit()
    next_a = a[next_a-1]
print(ans)
# Original solution
def solve():
    a = read()
    result = think(a)
    write(result)


def read():
    n = read_int(1)[0]
    a = []
    for _ in range(n):
        a.append(read_int(1)[0])
    return a


def read_int(n):
    return list([int(x) for x in read_line().split(' ')])[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(a):
    dummy = -1
    buttons_flash_info = list(a)
    buttons_flash_info.insert(0, dummy)

    index = 1
    visited = set()
    visited.add(index)
    push_count = 1
    while True:
        next_index = buttons_flash_info[index]
        index = next_index
        if index == 2:
            return push_count
        if index in visited:
            return -1
        push_count += 1
    return -1


def write(result):
    print(result)


if __name__ == '__main__':
    solve()

# Optimized solution
def solve():
    a = read()
    result = think(a)
    write(result)


def read():
    n = read_int(1)[0]
    a = []
    for _ in range(n):
        a.append(read_int(1)[0])
    return a


def read_int(n):
    return list([int(x) for x in read_line().split(' ')])[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(a):
    dummy = -1
    flag_for_not_visited = 0
    flag_for_visited = 1

    buttons_flash_info = list(a)
    buttons_flash_info.insert(0, dummy)

    index = 1
    visited = [flag_for_not_visited for i in range(len(buttons_flash_info))]
    visited[index] = flag_for_visited
    push_count = 1
    while True:
        next_index = buttons_flash_info[index]
        index = next_index
        if index == 2:
            return push_count
        if visited[index] == flag_for_visited:
            return -1
        visited[index] = flag_for_visited
        push_count += 1
    return -1


def write(result):
    print(result)


if __name__ == '__main__':
    solve()
# Original solution
# coding: utf-8

N = int(eval(input()))

pushed = []
button = [0] * (N + 1)

for i in range(1, N+1):
    button[i] = int(eval(input()))

push_button = 1
for _ in range(1, N+1):
    if push_button in pushed:
        print("-1")
        exit(0)
    elif push_button == 2:
        print((len(pushed)))
        exit(0)
    else:
        pushed.append(push_button)
        push_button = button[push_button]


# Optimized solution
# coding: utf-8

N = int(eval(input()))

pushed = []
button = [0] * (N + 1)

for i in range(1, N+1):
    button[i] = int(eval(input()))

push_button = 1
for _ in range(1, N+1):
    if push_button == 2:
        print((len(pushed)))
        exit(0)
    else:
        pushed.append(push_button)
        push_button = button[push_button]

print("-1")
# Original solution
n=int(eval(input()))
a=[]
cnt=0
now=0
for i in range(n):
  a.append(int(eval(input()))-1)
flg=False
for i in range(n):
  if now==1:
    flg=True
    break
  now=a[now]
  cnt+=1
print((cnt if flg else -1))  

# Optimized solution
N,*a = list(map(int,open(0).read().split()))
a.insert(0,0)
flg = False
on = 1
cnt = 0
for i in range(N):
    on = a[on]
    cnt += 1
    if on == 2:
        print(cnt)
        exit()
print((-1))
# Original solution
n = int(eval(input()))
A = [int(eval(input())) for i in range(n)]

M = [1]
i = 1
while True:
  M.append(A[i-1])
  i = A[i-1]
  if 2 in M:
    print((len(M)-1))
    break
  else:
    for j in range(len(M)):
      if M.count(M[j]) > 1:
        print("-1")
        exit()
    

# Optimized solution
n = int(eval(input()))

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

A = [0] + A
count = 0
light = 1
if 2 not in A:
  print("-1")
  exit()
else:
  while light != 2 and count <= n:
    count += 1
    light = A[light] #A = [0] + Aとしているので，これでよい．light-1としなくてよい．

if count <= n:
  print(count)
else:
  print("-1")
# Original solution
N = int(eval(input()))
a = [int(eval(input())) for i in range(N)]
now = a[0]
count = 1
c = 0
for i in range(10**6):
    if now != 2:
        count += 1
        now = a[now - 1]
    else:
        print(count)
        c = 1
        break
if c == 0:
    print((-1))


# Optimized solution
N = int(eval(input()))
a = [0] + [int(eval(input())) for _ in range(N)]
x = 1
visited = set([1])
answer = 0

while True:
  x = a[x]
  if x in visited:
    answer = -1
    break
  visited.add(x)
  answer += 1
  if x == 2:
    break
    
print(answer)
# Original solution
N = int(eval(input()))
A = [int(eval(input()))-1 for _ in range(N)]



done = []
light = 0
flag = 0
ans = -1
cnt = 0

while flag == 0:
  done.append(light)
  light = A[light]
  cnt += 1

  if light == 1:
    ans = cnt
    flag = 1
  if light in done:
    flag = 1
print(ans)

# Optimized solution
N = int(eval(input()))
A = [int(eval(input()))-1 for _ in range(N)]

light = 0
ans = -1
for n in range(1,N+1):
  light = A[light]
  if light ==1:
    ans = n
    break
print(ans)
# Original solution
N = eval(input())
arr = [eval(input())-1 for i in range(N)]

now = 0
count = 0
while (now != 1):
    count += 1
    now = arr[now]
    if count > 100000:
        count = -1
        break
print(count)


# Optimized solution
N = int(eval(input()))
src = [int(eval(input())) for i in range(N)]
now = 0
for i in range(N):
    a = src[now] - 1
    if a == 1:
        print((i+1))
        exit()
    now = a
print((-1))
# Original solution
N = int(eval(input()))
src = [int(eval(input())) for i in range(N)]
now = 0
for i in range(N):
    a = src[now] - 1
    if a == 1:
        print((i+1))
        exit()
    now = a
print((-1))

# Optimized solution
N = int(eval(input()))
src = [int(eval(input())) for i in range(N)]

now = src[0] - 1
for i in range(N):
    if now == 1:
        print((i+1))
        exit()
    now = src[now] - 1
print((-1))
# Original solution
N = int(eval(input()))
a = list(int(eval(input())) for i in range(N))

tmp = 1

for i in range(N):
    tmp = a[tmp-1]
    if tmp == 2:
        print((i + 1))
        exit()

print((-1))

# Optimized solution
N = int(eval(input()))
a = [0] * N
for i in range(N):
    a[i] = int(eval(input()))


tmp = 1
for i in range(N):
    tmp = a[tmp-1]
    if tmp == 2:
        print((i + 1))
        exit()

print((-1))
# Original solution
n = int(eval(input()))
a = []
hist = []
for i in range(n):
    a.append(int(eval(input())))
pos = 1
count = 0
while True:
    if pos == 2:
        print(count)
        break
    if pos in hist:
        print((-1))
        break
    hist.append(pos)
    pos = a[pos-1]
    count += 1

# Optimized solution
N = int(eval(input()))
a = [int(eval(input())) for i in range(N)]
ans = 0
turn = 1
while True:
    if a[turn-1] == 2:
        ans += 1
        break
    if a[turn-1] == 0:
        ans = -1
        break
    next_turn = a[turn-1]
    a[turn-1] = 0
    ans += 1
    turn = next_turn

print(ans)
# Original solution
n=int(eval(input()))
l=[int(eval(input())) for i in range(n)]
a=1
ans=0
while a!=2:
    a=l[a-1]
    ans+=1
    if a==1:
        ans=-1
        break
print(ans)
    

# Optimized solution
n=int(eval(input()))
l=[int(eval(input())) for i in range(n)]
a=1
ans=0
while a!=2:
    a=l[a-1]
    ans+=1
    if ans==n+1:
        ans=-1
        break
print(ans)
# Original solution
n = int(eval(input()))
lst = [int(eval(input())) for _ in range(n)]
blst = []
i = 1
n = 0
while i not in blst:
    blst.append(i)
    i = lst[i - 1]
    n += 1
    if i == 2:
        print(n)
        quit()
print((-1))

# Optimized solution
n = int(eval(input()))
lst = [int(eval(input())) for _ in range(n)]
i = 1
c = 0
for _ in range(n):
    i = lst[i - 1]
    c += 1
    if i == 2:
        print(c)
        quit()
print((-1))
# Original solution
n = int(eval(input()))
lst = [int(eval(input())) for _ in range(n)]
i = 1
c = 0
for _ in range(n):
    i = lst[i - 1]
    c += 1
    if i == 2:
        print(c)
        quit()
print((-1))

# Optimized solution
n = int(eval(input()))
lst = [int(eval(input())) for _ in range(n)]
i = 1

for c in range(n):
    i = lst[i - 1]
    if i == 2:
        print((c + 1))
        quit()
print((-1))
# Original solution
n = int(eval(input()))
lst = [int(eval(input())) for _ in range(n)]
i = lst[0]
for c in range(n):
    if i == 2:
        print((c + 1))
        quit()
    i = lst[i - 1]
print((-1))

# Optimized solution
import sys

n = int(eval(input()))
lst = [int(i) for i in sys.stdin]
i = lst[0]
for c in range(n):
    if i == 2:
        print((c + 1))
        quit()
    i = lst[i - 1]
print((-1))
# Original solution
N = int(eval(input()))
A = [int(eval(input())) for _ in range(N)]
pushed = [False] * N
pushed[0] = True
button = 0
count = 0
while True:
    count += 1
    button = A[button] - 1
    if button == 1:
        print(count)
        exit()
    if pushed[button]:
        print((-1))
        exit()


# Optimized solution
N = int(eval(input()))
A = [int(eval(input())) for _ in range(N)]
pushed = [False] * N
pushed[0] = True
button = 0
count = 0
while True:
    count += 1
    button = A[button] - 1
    if button == 1:
        print(count)
        exit()
    if pushed[button]:
        print((-1))
        exit()
    pushed[button] = True
# Original solution
import sys;sys.setrecursionlimit(9**9);*A,=list(map(int,open(0)))
def f(i,t):
 if i==2:return len(t)
 if i in t:return -1
 return f(A[i],t+[i])
print((f(1,[])))

# Optimized solution
*A,=list(map(int,open(0)));i=1;t=[0]*A[0]+[0]
while i-2and t[i]==0:t[i]=1;i=A[i]
print((-1if i-2else sum(t)))
# Original solution
import sys
N = int(eval(input()))
a = [int(eval(input())) for i in range(N)]
d = [0]
b = 0
c = 0
while True:
    if b == 1:
        print(c)
        sys.exit()
    c += 1
    b = a[b] - 1
    if b in d:
        print('-1')
        sys.exit()
    d.append(b)

# Optimized solution
import sys
N = int(eval(input()))
a = [int(eval(input())) for i in range(N)]
d = [0] * N
b = 0
c = 0
while True:
    if b == 1:
        print(c)
        sys.exit()
    c += 1
    b = a[b] - 1
    if d[b] == 1:
        print('-1')
        sys.exit()
    d[b] = 1
# Original solution
import sys
N = int(eval(input()))
a = [int(eval(input())) for i in range(N)]
d = [0] * N
b = 0
c = 0
while True:
    if b == 1:
        print(c)
        sys.exit()
    c += 1
    b = a[b] - 1
    if d[b] == 1:
        print('-1')
        sys.exit()
    d[b] = 1


# Optimized solution
N = int(eval(input()))
a = [int(eval(input())) for _ in range(N)]
s = [0] * N
c = 0
OK = False
n = 0
while True:
    c += 1
    n = a[n] - 1
    if n == 1:
        OK = True
        break
    if s[n] == 1:
        break
    s[n] = 1
if OK:
    print(c)
else:
    print((-1))