Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
n, a, b = map(int, input().split()))
print(sum(i for i in range(1 + n+1) if a <= sum(map(int, str(i)) <=b)))

Fixed submission:
n,a,b = map(int, input().split())
print(sum(i for i in range(1, n+1) if a <= sum(map(int, str(i))) <= b))

Buggy python submission (verdict: Runtime Error):
n,a,b = map(int, input().split())
ans = 0

def find_sum(x):
    count = 0
    while x > 0:
        count += x % 10
        x = x // 10

    return count

for i in range(1, n+1):
    count = find_sum(i)
    if A <= count <= B:
        ans += n

print(ans)

Fixed submission:
n,a,b = map(int, input().split())
print(sum(i for i in range(1, n+1) if a <= sum(map(int, str(i))) <= b))
Buggy python submission (verdict: Runtime Error):
S = input()
if re.match("^(dream|dreamer|erase|eraser)+$", S):
    print("Yes")
else:
    print("No")


Fixed submission:
import re
S = input()
'''
メタ文字：
^ = 文字列の先頭、
| = OR
+ = 直前のパターンを1回以上繰り返し、
$ = 文字列の末尾
'''
if re.match("^(dream|dreamer|erase|eraser)+$", S):
    print('YES')
else:
    print('NO')

Buggy python submission (verdict: Runtime Error):
S = input()
if re.match("^(dream|dreamer|erase|eraser)+$", s):
    print("Yes")
else:
    print("No")


Fixed submission:
import re
S = input()
'''
メタ文字：
^ = 文字列の先頭、
| = OR
+ = 直前のパターンを1回以上繰り返し、
$ = 文字列の末尾
'''
if re.match("^(dream|dreamer|erase|eraser)+$", S):
    print('YES')
else:
    print('NO')

Buggy python submission (verdict: Runtime Error):
import re
s = input()
if re.match("^(dream|dreamer|erase|eraser)+$, s):
    print("Yes")
else:
    print("No")

Fixed submission:
import re
S = input()
'''
メタ文字：
^ = 文字列の先頭、
| = OR
+ = 直前のパターンを1回以上繰り返し、
$ = 文字列の末尾
'''
if re.match("^(dream|dreamer|erase|eraser)+$", S):
    print('YES')
else:
    print('NO')
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n, k = [int(e) for e in input().split()]
    A = [int(e) for e in input().split()]

    s = A[0]
    for i in range(1, k):
        s *= A[i]

    prev = s
    for i in range(n - k):
        s //= A[i]
        s *= A[k + i]

        if prev < s:
            print('Yes')
        else:
            print('No')

        prev = s


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    n, k = [int(e) for e in input().split()]
    A = [int(e) for e in input().split()]

    for i in range(n - k):
        if A[i] < A[k + i]:
            print('Yes')
        else:
            print('No')


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n = int(input())
    x = input()

    A = [0] * n
    nx = int(x, 2)
    c1 = x.count('1')
    for i in range(n):
        c1s = c1
        nxs = nx
        if x[i] == '0':
            c1s += 1
            nxs += 2 ** (n - i - 1)
        else:
            c1s -= 1
            nxs -= 2 ** (n - i - 1)

        while c1s > 0:
            nxs %= c1s
            c1s = format(nxs, 'b').count('1')
            A[i] += 1

    for a in A:
        print(a)


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    n = int(input())
    x = input()

    c1 = x.count('1')
    c1p, c1m = c1 + 1, c1 - 1
    sp = sum(pow(2, n - i - 1, c1p) for i, c in enumerate(x) if c == '1')
    sm = None
    if c1m > 0:
        sm = sum(pow(2, n - i - 1, c1m) for i, c in enumerate(x) if c == '1')

    A = [0] * n
    for i in range(n):
        nx = None
        if x[i] == '0':
            nx = (sp + pow(2, n - i - 1, c1p)) % c1p
        else:
            if sm is None:
                continue

            nx = (sm - pow(2, n - i - 1, c1m)) % c1m

        A[i] += 1

        while nx > 0:
            nx %= format(nx, 'b').count('1')
            A[i] += 1

    for a in A:
        print(a)


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
import math


def main():
    n = int(input())

    A = [2] * (n + 1)
    
    ans = 1
    for i in range(2, n + 1):
        d = math.isqrt(i)
        A[i] = d + 1
        ans += i * (A[d] + 1)

    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    n = int(input())

    ans = 0
    for i in range(1, n + 1):
        d = n // i
        ans += (i + (i * d)) * d // 2

    print(ans)


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n = int(input())
    A = [int(e) for e in input().split()]

    A.sort()

    D = set()
    prev = -1
    for i in range(n):
        if A[i] == prev:
            if len([d for d in D if A[i] % d == 0]) == 0:
                D.add(A[i])

            A[i - 1] = -1
            A[i] = -1
        else:
            prev = A[i]

    A = [a for a in A if a != -1]
    ans = 0
    for i in range(len(A)):
        if len([d for d in D if A[i] % d == 0]) == 0:
            ans += 1
            D.add(A[i])

    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    n = int(input())
    A = [int(e) for e in input().split()]

    ma = max(A)
    N = [0] * (10 ** 6 + 1)
    for a in A:
        for i in range(a, ma + 1, a):
            N[i] += 1

    ans = 0
    for a in A:
        if N[a] == 1:
            ans += 1

    print(ans)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n = int(input())
    A = [int(e) for e in input().split()]

    A.sort()

    D = set()
    prev = -1
    for i in range(n):
        if A[i] == prev:
            D.add(A[i])
            A[i - 1] = -1
            A[i] = -1
        else:
            prev = A[i]

    A = [a for a in A if a != -1]
    ans = 0
    for i in range(len(A)):
        if len([d for d in D if A[i] % d == 0]) == 0:
            ans += 1
            D.add(A[i])

    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    n = int(input())
    A = [int(e) for e in input().split()]

    ma = max(A)
    N = [0] * (10 ** 6 + 1)
    for a in A:
        for i in range(a, ma + 1, a):
            N[i] += 1

    ans = 0
    for a in A:
        if N[a] == 1:
            ans += 1

    print(ans)


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    s = input()
    mod = 2019

    n = 0
    R = [0] * mod
    R[0] += 1
    for i in range(len(s)):
        n += int(s[-i - 1]) * (10 ** i) % mod
        R[n % mod] += 1

    print(sum([r * (r - 1) // 2 for r in R]))


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    s = input()
    mod = 2019

    n = 0
    R = [0] * mod
    R[0] += 1
    for i in range(len(s)):
        n += int(s[-i - 1]) * pow(10, i, mod)
        R[n % mod] += 1

    print(sum([r * (r - 1) // 2 for r in R]))


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    s = input()
    mod = 2019

    s2 = ''
    R = [0] * mod
    R[0] += 1
    for i in range(len(s)):
        s2 = ''.join([s[-i - 1], s2])
        R[int(s2) % mod] += 1

    print(sum([r * (r - 1) // 2 for r in R]))


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    s = input()
    mod = 2019

    n = 0
    R = [0] * mod
    R[0] += 1
    for i in range(len(s)):
        n += int(s[-i - 1]) * pow(10, i, mod)
        R[n % mod] += 1

    print(sum([r * (r - 1) // 2 for r in R]))


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    s = input()
    mod = 2019

    s2 = ''
    R = [0] * mod
    R[0] += 1
    for i in range(len(s)):
        s2 = f'{s[-i - 1]}{s2}'
        R[int(s2) % mod] += 1

    print(sum([r * (r - 1) // 2 for r in R]))


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    s = input()
    mod = 2019

    n = 0
    R = [0] * mod
    R[0] += 1
    for i in range(len(s)):
        n += int(s[-i - 1]) * pow(10, i, mod)
        R[n % mod] += 1

    print(sum([r * (r - 1) // 2 for r in R]))


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    s = input()
    mod = 2019

    R = [0] * mod
    R[0] += 1
    for i in range(len(s)):
        R[int(s[-i - 1:]) % mod] += 1

    print(sum([r * (r - 1) // 2 for r in R]))


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    s = input()
    mod = 2019

    n = 0
    R = [0] * mod
    R[0] += 1
    for i in range(len(s)):
        n += int(s[-i - 1]) * pow(10, i, mod)
        R[n % mod] += 1

    print(sum([r * (r - 1) // 2 for r in R]))


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
from itertools import combinations

def main():
    n, x, y = [int(e) for e in input().split()]

    D = dict()
    D[1] = {2}
    D[n] = {n - 1}
    for i in range(2, n):
        D[i] = {i - 1, i + 1}

    D[x].add(y)
    D[y].add(x)

    A = [0]*(n - 1)
    for c in combinations(range(1, n + 1), 2):
        Q = deque()
        Q.extend(D[c[0]])
        cnt = 0
        end = False
        while not end:
            cnt += 1
            Qex = set()
            while len(Q) != 0:
                e = Q.popleft()
                if e == c[1]:
                    Q.clear()
                    end = True
                    break

                Qex |= D[e]

            Q.extend(Qex)

        A[cnt - 1] += 1

    for a in A:
        print(a)

if __name__ == "__main__":
    main()


Fixed submission:
from itertools import combinations

def main():
    n, x, y = [int(e) for e in input().split()]

    A = [0]*(n - 1)
    for c in combinations(range(1, n + 1), 2):
        cnt = min(abs(c[1] - c[0]), abs(c[1] - y) + abs(c[0] - x) + 1, abs(c[1] - x) + abs(c[0] - y) + 1)
        A[cnt - 1] += 1

    for a in A:
        print(a)

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter

def main():
    n = int(input())
    A = [int(e) for e in input().split()]

    Da = Counter(A)
    Ds = sorted(Da.items(), key=lambda x: x[1])
    lds = len(Ds)
    c1 = Ds.count(1)

    for i in range(n):
        cnt = 0
        for j in range(lds - c1):
            cnt += Ds[j][1]*(Ds[j][1] - 1)//2

        cnt -= Da[A[i]] - 1
        print(cnt)

if __name__ == "__main__":
    main()


Fixed submission:
from collections import Counter

def main():
    n = int(input())
    A = [int(e) for e in input().split()]

    Da = Counter(A)
    cnt = 0
    for v in Da.values():
        cnt += v*(v - 1)//2

    for i in range(n):
        print(cnt - (Da[A[i]] - 1))

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter

def main():
    n = int(input())
    A = [int(e) for e in input().split()]

    Da = Counter(A)
    Ds = sorted(Da.items(), key=lambda x: x[1])

    for i in range(n):
        cnt = 0
        for j in range(len(Ds) - Ds.count(1)):
            cnt += Ds[j][1]*(Ds[j][1] - 1)//2

        cnt -= Da[A[i]] - 1
        print(cnt)

if __name__ == "__main__":
    main()


Fixed submission:
from collections import Counter

def main():
    n = int(input())
    A = [int(e) for e in input().split()]

    Da = Counter(A)
    cnt = 0
    for v in Da.values():
        cnt += v*(v - 1)//2

    for i in range(n):
        print(cnt - (Da[A[i]] - 1))

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    s = input()
    q = int(input())

    rev = False
    for i in range(q):
        tfc = input().split()

        if tfc[0] == '1':
            rev = not rev
        else:
            f = tfc[1]
            c = tfc[2]
            if (f == '1' and not rev) or (f != '1' and rev):
                s = c + s
            else:
                s = s + c

    if rev:
        s = s[::-1]

    print(s)

if __name__ == "__main__":
    main()


Fixed submission:
from collections import deque

def main():
    s = input()
    q = int(input())

    Q = deque(s)
    rev = False
    for i in range(q):
        tfc = input().split()

        if tfc[0] == '1':
            rev = not rev
        else:
            f = tfc[1]
            c = tfc[2]
            if (f == '1' and not rev) or (f != '1' and rev):
                Q.appendleft(c)
            else:
                Q.append(c)

    if rev:
        print(''.join(list(Q)[::-1]))
    else:
        print(''.join(Q))

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    s = input()
    q = int(input())

    for i in range(q):
        tfc = input().split()

        if tfc[0] == '1':
            s = s[::-1]
        else:
            f = tfc[1]
            c = tfc[2]
            if f == '1':
                s = c + s
            else:
                s = s + c

    print(s)

if __name__ == "__main__":
    main()


Fixed submission:
from collections import deque

def main():
    s = input()
    q = int(input())

    Q = deque(s)
    rev = False
    for i in range(q):
        tfc = input().split()

        if tfc[0] == '1':
            rev = not rev
        else:
            f = tfc[1]
            c = tfc[2]
            if (f == '1' and not rev) or (f != '1' and rev):
                Q.appendleft(c)
            else:
                Q.append(c)

    if rev:
        print(''.join(list(Q)[::-1]))
    else:
        print(''.join(Q))

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
def main():
    n, m = [int(e) for e in input().split()]
    S, C = zip(*[[int(e) for e in input().split()] for i in range(m)])

    ans = 0
    D = {}
    for si, ci in zip(S, C):
        if D.get(si) == ci:
            continue

        if D.get(si) is not None:
            print(-1)
            exit()

        ans += 10**(n - si) * ci
        D[si] = ci

    if len(str(ans)) != n:
        print(-1)
        exit()

    print(ans)

if __name__ == "__main__":
    main()


Fixed submission:
def main():
    n = int(input())

    ans = n//2 + n%2
    print(ans)

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
def main():
    n, m = [int(e) for e in input().split()]
    S, C = zip(*[[int(e) for e in input().split()] for i in range(m)])

    ans = 0
    D = {}
    for si, ci in zip(S, C):
        if D.get(si) == ci:
            continue

        if D.get(si) is not None:
            print(-1)
            exit()

        ans += 10**(n - si) * ci
        D[si] = ci

    if len(str(ans)) != n:
        print(-1)
        exit()

    print(ans)

if __name__ == "__main__":
    main()


Fixed submission:
def main():
    n, m = [int(e) for e in input().split()]
    if n == 1 and m == 0:
        print(0)
        exit()

    if m == 0:
        print(10**(n - 1))
        exit()

    S, C = zip(*[[int(e) for e in input().split()] for i in range(m)])

    ans = 0
    D = {}
    for si, ci in zip(S, C):
        if D.get(si) == ci:
            continue

        if n != 1 and si == 1 and ci == 0:
            print(-1)
            exit()

        if D.get(si) is not None:
            print(-1)
            exit()

        ans += 10**(n - si) * ci
        D[si] = ci

    if len(str(ans)) > n:
        print(-1)
        exit()

    if len(str(ans)) < n:
        ans += 10**(n - 1)

    print(ans)

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
def main():
    n, q = [int(e) for e in input().split()]
    AB = [[int(e) for e in input().split()] for i in range(n - 1)]
    PX = [[int(e) for e in input().split()] for i in range(q)]

    V = dict()
    for a, b in AB:
        V[b] = a

    S = [0] * (n + 1)
    for p, x in PX:
        S[p] += x

    Ans = [0] * n
    Ans[0] = S[1]
    for i in range(1, n):
        Ans[i] += S[i + 1] + Ans[V[i + 1] - 1]

    print(*Ans)


if __name__ == '__main__':
    main()


Fixed submission:
from collections import deque, defaultdict


def main():
    n, q = [int(e) for e in input().split()]
    AB = [[int(e) for e in input().split()] for i in range(n - 1)]
    PX = [[int(e) for e in input().split()] for i in range(q)]

    G = defaultdict(set)
    for a, b in AB:
        G[a].add(b)
        G[b].add(a)

    Ans = [0] * (n + 1)
    for p, x in PX:
        Ans[p] += x

    Q = deque([1])
    Visited = [False] * (n + 1)
    Visited[1] = True
    while len(Q) > 0:
        v = Q.popleft()
        for vc in G[v]:
            if Visited[vc]:
                continue

            Ans[vc] += Ans[v]
            Q.append(vc)
            Visited[vc] = True

    print(*Ans[1:])


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
import heapq


def main():
    n, m = [int(e) for e in input().split()]
    AB = [[int(e) for e in input().split()] for i in range(n)]

    AB.sort(key=lambda x: x[0])
    H = []
    ans = 0
    mi = 1
    for a, b in AB:
        while a > mi:
            if len(H) > 0:
                heapq.heapify(H)
                ans += -heapq.heappop(H)

            mi += 1
            if mi > m:
                break

        if a > m:
            break

        H.append(-b)

    while mi <= m and len(H) > 0:
        heapq.heapify(H)
        ans += -heapq.heappop(H)
        mi += 1

    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
import heapq


def main():
    n, m = [int(e) for e in input().split()]
    AB = [[int(e) for e in input().split()] for i in range(n)]

    AB.sort(key=lambda x: x[0])
    H = []
    ans = 0
    mi = 1
    for a, b in AB:
        while a > mi:
            if len(H) > 0:
                ans += -heapq.heappop(H)

            mi += 1
            if mi > m:
                break

        if a > m:
            break

        heapq.heappush(H, -b)

    while mi <= m and len(H) > 0:
        ans += -heapq.heappop(H)
        mi += 1

    print(ans)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import heapq


def main():
    n, m = [int(e) for e in input().split()]
    AB = [[int(e) for e in input().split()] for i in range(n)]

    AB.sort(key=lambda x: x[0])

    H = []
    abi = 0
    ans = 0
    for mi in range(1, m + 1):
        while abi < n:
            a, b = AB[abi]
            if a == mi:
                abi += 1
                H.append(-b)
            else:
                break

        if len(H) > 0:
            heapq.heapify(H)
            ans += -heapq.heappop(H)

    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
import heapq


def main():
    n, m = [int(e) for e in input().split()]
    AB = [[int(e) for e in input().split()] for i in range(n)]

    AB.sort(key=lambda x: x[0])
    H = []
    ans = 0
    mi = 1
    for a, b in AB:
        while a > mi:
            if len(H) > 0:
                ans += -heapq.heappop(H)

            mi += 1
            if mi > m:
                break

        if a > m:
            break

        heapq.heappush(H, -b)

    while mi <= m and len(H) > 0:
        ans += -heapq.heappop(H)
        mi += 1

    print(ans)


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
n, a, b, c = [int(e) for e in input().split()]
L = [int(input()) for i in range(n)]

def dfs(A, B, C, O, lst, mi):
    if len(lst) == 0:
        if len(A) == 0 or len(B) == 0 or len(C) == 0:
            return mi

        mp = 0
        mp += (len(A) - 1)*10 + abs(a - sum(A))
        mp += (len(B) - 1)*10 + abs(b - sum(B))
        mp += (len(C) - 1)*10 + abs(c - sum(C))
        return min(mp, mi)

    l = lst.pop()
    mi = dfs(A + [l], B.copy(), C.copy(), O.copy(), lst.copy(), mi)
    mi = dfs(A.copy(), B + [l], C.copy(), O.copy(), lst.copy(), mi)
    mi = dfs(A.copy(), B.copy(), C + [l], O.copy(), lst.copy(), mi)
    mi = dfs(A.copy(), B.copy(), C.copy(), O + [l], lst.copy(), mi)

    return mi

def main():
    print(dfs([], [], [], [], L.copy(), float('inf')))

if __name__ == "__main__":
    main()


Fixed submission:
n, a, b, c = [int(e) for e in input().split()]
L = [int(input()) for i in range(n)]

def dfs(A, B, C, O, lst, mi):
    if len(lst) == 0:
        if len(A) == 0 or len(B) == 0 or len(C) == 0:
            return mi

        mp = 0
        mp += (len(A) - 1)*10 + abs(a - sum(A))
        mp += (len(B) - 1)*10 + abs(b - sum(B))
        mp += (len(C) - 1)*10 + abs(c - sum(C))
        return min(mp, mi)

    l = lst.pop()
    mi = dfs(A + [l], B.copy(), C.copy(), O.copy(), lst.copy(), mi)
    mi = dfs(A.copy(), B + [l], C.copy(), O.copy(), lst.copy(), mi)
    mi = dfs(A.copy(), B.copy(), C + [l], O.copy(), lst.copy(), mi)
    mi = dfs(A.copy(), B.copy(), C.copy(), O + [l], lst.copy(), mi)

    return mi

def main():
    print(dfs([], [], [], [], L.copy(), float('inf')))

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
import math

def main():
    n, x = [int(e) for e in input().split()]
    X = [int(e) for e in input().split()]

    ans = abs(x - X[0])
    for xi in X:
        ans = math.gcd(abs(x - xi), ans)

    print(ans)

if __name__ == "__main__":
    main()


Fixed submission:
import fractions

def main():
    n, x = [int(e) for e in input().split()]
    X = [int(e) for e in input().split()]

    ans = abs(x - X[0])
    for xi in X:
        ans = fractions.gcd(abs(x - xi), ans)

    print(ans)

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
import statistics

def main():
    n = int(input())
    A = [int(e) for e in input().split()]

    B = [a - ai - 1 for ai, a in enumerate(A)]
    m = int(statistics.median(B))
    ans = sum([abs(b - m) for b in B])
    print(ans)

if __name__ == "__main__":
    main()


Fixed submission:
import statistics

def main():
    n = int(input())
    A = [int(e) for e in input().split()]

    B = [a - ai - 1 for ai, a in enumerate(A)]
    m = int(statistics.median(B))
    ans = sum([abs(b - m) for b in B])
    print(ans)

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n = int(input())
    s = input()

    W = []
    wcum = 0
    for c in s:
        W.append(wcum)
        if c == 'W':
            wcum += 1

    E = []
    ecum = 0
    for c in s[::-1]:
        E.insert(0, ecum)
        if c == 'E':
            ecum += 1

    ans = min([W[i] + E[i] for i in range(n)])
    print(ans)

if __name__ == "__main__":
    main()


Fixed submission:
def main():
    n = int(input())
    s = input()

    cum = s[1:].count('E')
    ans = cum
    for i in range(1, n):
        if s[i - 1] == 'W':
            cum += 1
        if s[i] == 'E':
            cum -= 1

        ans = min(cum, ans)
    print(ans)

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n = int(input())
    a = [int(input()) for i in range(n)]

    btn = 1
    hist = []
    while True:
        if btn == 2:
            print(len(hist))
            break

        if btn in hist:
            print(-1)
            break

        hist.append(btn)
        btn = a[btn - 1]


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    n = int(input())
    a = [int(input()) for i in range(n)]

    btn = 1
    hist = set()
    while True:
        if btn == 2:
            print(len(hist))
            break

        if btn in hist:
            print(-1)
            break

        hist.add(btn)
        btn = a[btn - 1]


if __name__ == "__main__":
    main()