Problem p02761 (python)

Fixed submission:
import sys
N, M = map(int,input().split())
s = []
c = []
for m in range(M):
    S, C = map(int,input().split())
    s.append(S)
    c.append(C)


for i in range(10 ** N):
    Str = str(i)

    if len(Str) == N and all([Str[s[m] - 1] == str(c[m]) for m in range(M)]):
        print(Str)
        sys.exit(0)

print(-1)

Buggy submission (verdict: Runtime Error):
import sys
N, M = map(int, input().split())

if N == 1:
    sc = []
    for m in range(M):
        s, c = map(int, input().split())
        sc.append(c)
    sc = set(sc)
    if len(sc) == 0:
        print(0)
        sys.exit(0)
    elif len(sc) == 1:
        print(sc[0])
        sys.exit(0)
    else:
        print(-1)
        sys.exit(0)
        
if N == 2:
    sc = []
    for m in range(M):
        s, c = map(int, input().split())
        sc.append([s, c])
    sc = list(map(list, set(map(tuple, sc))))
    
    l1 = []
    for m in range(M):
        if sc[m][0] == 1 and sc[m][1] > 0:
            l1.append(sc[m][1])
    if len(l1) == 0 or len(l1) > 1:
        print(-1)
        sys.exit(0)
    else:
        lank1 = min(l1)
    
    l2 = []
    for m in range(M):
        if sc[m][0] == 2:
            l2.append(sc[m][1])
    if len(l2) == 0:
        lank2 = 0
    elif len(l2) == 1:
        lank2 = min(l2)
    else:
        print(-1)
        sys.exit(0)
    
    ans = lank1 * 10 + lank2
    print(ans)
    sys.exit(0)
    
if N == 3:
    sc = []
    for m in range(M):
        s, c = map(int, input().split())
        sc.append([s, c])
    sc = list(map(list, set(map(tuple, sc))))    
    
    l1 = []
    for m in range(M):
        if sc[m][0] == 1 and sc[m][1] > 0:
            l1.append(sc[m][1])
    if len(l1) == 0 or len(l1) > 1:
        print(-1)
        sys.exit(0)
    else:
        lank1 = min(l1)
    
    l2 = []
    for m in range(M):
        if sc[m][0] == 2:
            l2.append(sc[m][1])
    if len(l2) == 0:
        lank2 = 0
    elif len(l2) == 1:
        lank2 = min(l2)
    else:
        print(-1)
        sys.exit(0)
    
    l3 = []
    for m in range(M):
        if sc[m][0] == 3:
            l3.append(sc[m][1])
    if len(l3) == 0:
        lank3 = 0
    elif len(l3) == 1:
        lank3 = min(l3)
    else:
        print(-1)
        sys.exit(0)
    
    ans = lank1 * 100 + lank2 * 10 + lank3
    print(ans)
    sys.exit(0)

Buggy submission (verdict: Runtime Error):
import sys
N, M = map(int, input().split())

if N == 1:
    sc = []
    for m in range(M):
        s, c = map(int, input().split())
        sc.append(c)
    print(min(sc))
    sys.exit(0)

if N == 2:
    sc = []
    for m in range(M):
        s, c = map(int, input().split())
        sc.append([s, c])
    
    l1 = []
    for m in range(M):
        if sc[m][0] == 1 and sc[m][1] > 0:
            l1.append(sc[m][1])
    if len(l1) == 0:
        print(-1)
        sys.exit(0)
    else:
        lank1 = min(l1)
    
    l2 = []
    for m in range(M):
        if sc[m][0] == 2:
            l2.append(sc[m][1])
    if len(l2) == 0:
        lank2 = 0
    else:
        lank2 = min(l2)
    
    ans = lank1 * 10 + lank2
    print(ans)
    sys.exit(0)
    
if N == 3:
    sc = []
    for m in range(M):
        s, c = map(int, input().split())
        sc.append([s, c])
        
    l1 = []
    for m in range(M):
        if sc[m][0] == 1 and sc[m][1] > 0:
            l1.append(sc[m][1])
    if len(l1) == 0:
        print(-1)
        sys.exit(0)
    else:
        lank1 = min(l1)
    
    l2 = []
    for m in range(M):
        if sc[m][0] == 2:
            l2.append(sc[m][1])
    if len(l2) == 0:
        lank2 = 0
    else:
        lank2 = min(l2)
    
    l3 = []
    for m in range(M):
        if sc[m][0] == 3:
            l3.append(sc[m][1])
    if len(l3) == 0:
        lank3 = 0
    else:
        lank3 = min(l3)
    
    ans = lank1 * 100 + lank2 * 10 + lank3
    print(ans)
    sys.exit(0)