Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
a=[-1]*(N+1)
for i in range(1,N+1):
  a[i]=int(input())
pos = 1
prev=[1]  
for _ in range(N):
  if pos == 2:
    print(len(prev)-1)
    exit(0)
  pos = a[pos]
  if pos not in prev:
    prev.append(pos)
  

print(- 1)

Fixed submission:
N=int(input())
a=[-1]*(N+1)
for i in range(1,N+1):
  a[i]=int(input())
pos = 1
ans = 0
for _ in range(N):
  if pos == 2:
    print(ans)
    exit(0)
  pos = a[pos]
  ans += 1
  

print(- 1)

Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
a=[-1]*(N+1)
for i in range(1,N+1):
  a[i]=int(input())
pos = 1
prev=[1]  
while(pos!= 2):
  pos = a[pos]
  if pos in prev:
    print(-1)
    exit(0)
  else:
    prev.append(pos)
print(len(prev) - 1)

Fixed submission:
N=int(input())
a=[-1]*(N+1)
for i in range(1,N+1):
  a[i]=int(input())
pos = 1
ans = 0
for _ in range(N):
  if pos == 2:
    print(ans)
    exit(0)
  pos = a[pos]
  ans += 1
  

print(- 1)

Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
a=[-1]*(N+1)
b=[[] for i in range(N+1)]
for i in range(1,N+1):
  a[i]=int(input())
  b[a[i]].append(i)

prev=[2]  
count=0
button=2
while(button!=1):
  if len(b[button]) != 0:
    button = b[button].pop()
    if button not in prev:
      prev.append(button)
    else:
      button=prev[-1]
  else:
    prev.pop()
    if len(prev) == 0:
      print(-1)
      exit(0)
    button = prev[-1]
    continue

  
print(len(prev) - 1)

Fixed submission:
N=int(input())
a=[-1]*(N+1)
for i in range(1,N+1):
  a[i]=int(input())
pos = 1
ans = 0
for _ in range(N):
  if pos == 2:
    print(ans)
    exit(0)
  pos = a[pos]
  ans += 1
  

print(- 1)

Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
a=[-1]*(N+1)
b=[[] for i in range(N+1)]
for i in range(1,N+1):
  a[i]=int(input())
  b[a[i]].append(i)

prev=[2]  
count=0
button=2
while(button!=1):
  if len(b[button]) != 0:
    button = b[button].pop()
    if button not in prev:
      prev.append(button)
    else:
      button=prev[-1]
  else:
    if len(prev) == 0:
        print(-1)
        exit(0)
    if prev[-1]==button:
      prev.pop()
      if len(prev) == 0:
        print(-1)
        exit(0)
      button = prev[-1]
      continue
    
  
print(len(prev) - 1)

Fixed submission:
N=int(input())
a=[-1]*(N+1)
for i in range(1,N+1):
  a[i]=int(input())
pos = 1
ans = 0
for _ in range(N):
  if pos == 2:
    print(ans)
    exit(0)
  pos = a[pos]
  ans += 1
  

print(- 1)

Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
a=[-1]*(N+1)
b=[[] for i in range(N+1)]
for i in range(1,N+1):
  a[i]=int(input())
  b[a[i]].append(i)

prev=[2]  
count=0
button=2
while(button!=1):
  if len(b[button]) != 0:
    button = b[button].pop()
    if button not in prev:
      prev.append(button)
    else:
      button=prev[-1]
  else:
    if prev[-1]==button:
      prev.pop()
      if len(prev) == 0:
        print(-1)
        exit(0)
      button = prev[-1]
      continue
  
print(len(prev) - 1)

Fixed submission:
N=int(input())
a=[-1]*(N+1)
for i in range(1,N+1):
  a[i]=int(input())
pos = 1
ans = 0
for _ in range(N):
  if pos == 2:
    print(ans)
    exit(0)
  pos = a[pos]
  ans += 1
  

print(- 1)