Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
L = [int(input()) for _ in range(n)]
count = 0
next = 0
NG = [1]
while True:
  if L[next] in NG:
    print(-1)
    quit()
  elif L[next] == 2:
    print(count+1)
    quit()
  else:
    NG.append(L[next])
    next = L[next]-1
    count += 1

Fixed submission:
n = int(input())
a = [int(input()) for _ in range(n)]
cur = 1
cnt = 0
while True:
    if cur == 2:
        print(cnt)
        exit()
    elif cnt > n:
        print(-1)
        exit()
    else:
        cur = a[cur - 1]
        cnt += 1

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
L = sum([list(map(int, input().split())) for i in range(n)], [])
next = 1
count = 0
while True:
  if next == 2:
    print(count)
    exit()
  elif count > n:
    print(-1)
    exit()
  else:
    next = L[next-1]
    count += 1

Fixed submission:
n = int(input())
a = [int(input()) for _ in range(n)]
cur = 1
cnt = 0
while True:
    if cur == 2:
        print(cnt)
        exit()
    elif cnt > n:
        print(-1)
        exit()
    else:
        cur = a[cur - 1]
        cnt += 1

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
L = sum([list(map(int, input().split())) for i in range(n)], [])
next = 1
count = 0
while True:
  if next == 2:
    print(count)
    exit()
  elif count > n:
    print(-1)
    exit()    
  next = L[next-1]
  count += 1

Fixed submission:
n = int(input())
a = [int(input()) for _ in range(n)]
cur = 1
cnt = 0
while True:
    if cur == 2:
        print(cnt)
        exit()
    elif cnt > n:
        print(-1)
        exit()
    else:
        cur = a[cur - 1]
        cnt += 1

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
L = sum([list(map(int, input().split())) for i in range(n)], [])
next = 1
count = 0
for i in range(n):
  if next == 2:
    print(i)
    exit()
  next = L[next-1]
print(-1)

Fixed submission:
n = int(input())
a = [int(input()) for _ in range(n)]
cur = 1
cnt = 0
while True:
    if cur == 2:
        print(cnt)
        exit()
    elif cnt > n:
        print(-1)
        exit()
    else:
        cur = a[cur - 1]
        cnt += 1

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
L = sum([list(map(int, input().split())) for i in range(n)], [])
next = 1
count = 0
for i in range(n):
  if next == 2:
    print(i)
    quit()
  next = L[next-1]
print(-1)

Fixed submission:
n = int(input())
a = [int(input()) for _ in range(n)]
cur = 1
cnt = 0
while True:
    if cur == 2:
        print(cnt)
        exit()
    elif cnt > n:
        print(-1)
        exit()
    else:
        cur = a[cur - 1]
        cnt += 1

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
L = sum([list(map(int, input().split())) for i in range(n)], [])
next = 0
for i in range(len(L)):
  if L[next] == 2:
    print(i+1)
    quit()
  next = L[next]-1
print(-1)

Fixed submission:
n = int(input())
a = [int(input()) for _ in range(n)]
cur = 1
cnt = 0
while True:
    if cur == 2:
        print(cnt)
        exit()
    elif cnt > n:
        print(-1)
        exit()
    else:
        cur = a[cur - 1]
        cnt += 1

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
L = sum([list(map(int, input().split())) for i in range(n)], [])
count = 0
next = 0
for i in range(len(L)):
  if L[next] == 2:
    print(count+1)
    quit()
  next = L[next]-1
  count += 1
print(-1)

Fixed submission:
n = int(input())
a = [int(input()) for _ in range(n)]
cur = 1
cnt = 0
while True:
    if cur == 2:
        print(cnt)
        exit()
    elif cnt > n:
        print(-1)
        exit()
    else:
        cur = a[cur - 1]
        cnt += 1

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
L = sum([list(map(int, input().split())) for i in range(n)], [])
count = 0
next = 0
NG = [1]
while True:
  if L[next] in NG:
    print(-1)
    quit()
  elif L[next] == 2:
    print(len(NG))
    quit()
  else:
    NG.append(L[next])
    next = L[next]-1

Fixed submission:
n = int(input())
a = [int(input()) for _ in range(n)]
cur = 1
cnt = 0
while True:
    if cur == 2:
        print(cnt)
        exit()
    elif cnt > n:
        print(-1)
        exit()
    else:
        cur = a[cur - 1]
        cnt += 1

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
L = sum([list(map(int, input().split())) for i in range(n)], [])
count = 0
next = 0
NG = [1]
while True:
  if L[next] in NG:
    print(-1)
    quit()
  elif L[next] == 2:
    print(count+1)
    quit()
  else:
    NG.append(L[next])
    next = L[next]-1
    count += 1

Fixed submission:
n = int(input())
a = [int(input()) for _ in range(n)]
cur = 1
cnt = 0
while True:
    if cur == 2:
        print(cnt)
        exit()
    elif cnt > n:
        print(-1)
        exit()
    else:
        cur = a[cur - 1]
        cnt += 1

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
L = sum([list(map(int, input().split())) for i in range(n)], [])
count = 0
next = 0
NG = [1]
while True:
  if L[next] in NG:
    print(-1)
    quit()
  elif L[next] == 2:
    print(count+1)
    quit()
  else:
    NG.append(L[next])
    next = L[next]-1
    count += 1

Fixed submission:
n = int(input())
a = [int(input()) for _ in range(n)]
cur = 1
cnt = 0
while True:
    if cur == 2:
        print(cnt)
        exit()
    elif cnt > n:
        print(-1)
        exit()
    else:
        cur = a[cur - 1]
        cnt += 1