Buggy python submission (verdict: Time Limit Exceeded):
import sys
from copy import deepcopy
sys.setrecursionlimit(10 ** 9)

n = int(input())
lista = [[] for i in range(n + 1)]
ans = float('inf')
for i in range(n):
    a = int(input())
    lista[i + 1].append(a)
def dfs(now, ignore):
    global ans
    if now == 2:
        ans = min(ans, len(ignore) - 1)
    for i in lista[now]:
        if i in ignore:
            continue
        nignore = deepcopy(ignore)
        nignore.append(i)
        dfs(i, nignore)
dfs(1, [1])
print(-1 if ans == float('inf') else ans)

Fixed submission:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
import math
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7


#############
# Main Code #
#############

N = getN()
A = getArray(N)
for i in range(N):
    A[i] -= 1
ignore = [0] * N
ignore[0] = 1

now = 0
cnt = 0
while True:
    now = A[now]
    cnt += 1
    if ignore[now] == 1:
        print(-1)
        break
    ignore[now] = 1
    if now == 1:
        print(cnt)
        break

Buggy python submission (verdict: Runtime Error):
from copy import deepcopy
n = int(input())
lista = [[] for i in range(n + 1)]
ans = 10000000
for i in range(n):
    a = int(input())
    lista[i + 1].append(a)
def dfs(now, ignore):
    global ans
    if now == 2:
        ans = min(ans, len(ignore) - 1)
    for i in lista[now]:
        if i in ignore:
            continue
        nignore = deepcopy(ignore)
        nignore.append(i)
        dfs(i, nignore)
dfs(1, [1])
print(-1 if ans == 10000000 else ans)

Fixed submission:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
import math
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7


#############
# Main Code #
#############

N = getN()
A = getArray(N)
for i in range(N):
    A[i] -= 1
ignore = [0] * N
ignore[0] = 1

now = 0
cnt = 0
while True:
    now = A[now]
    cnt += 1
    if ignore[now] == 1:
        print(-1)
        break
    ignore[now] = 1
    if now == 1:
        print(cnt)
        break

Buggy python submission (verdict: Runtime Error):
from copy import deepcopy
n = int(input())
lista = [[] for i in range(n + 1)]
ans = float('inf')
for i in range(n):
    a = int(input())
    lista[i + 1].append(a)
def dfs(now, ignore):
    global ans
    if now == 2:
        ans = min(ans, len(ignore) - 1)
    for i in lista[now]:
        if i in ignore:
            continue
        nignore = deepcopy(ignore)
        nignore.append(i)
        dfs(i, nignore)
dfs(1, [1])
print(-1 if ans == float('inf') else ans)

Fixed submission:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
import math
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7


#############
# Main Code #
#############

N = getN()
A = getArray(N)
for i in range(N):
    A[i] -= 1
ignore = [0] * N
ignore[0] = 1

now = 0
cnt = 0
while True:
    now = A[now]
    cnt += 1
    if ignore[now] == 1:
        print(-1)
        break
    ignore[now] = 1
    if now == 1:
        print(cnt)
        break