Problem p02761 (python)

Fixed submission:
N,M = map(int,input().split())
values = []
for i in range(N):
    values.append([])
for i in range(M):
    s,c = map(int,input().split())
    values[s-1].append(c)

#同じ桁に2つ以上候補があるなら-1
for i in values:
    if len(set(i)) >= 2:
        print(-1)
        exit()

#最上位桁に候補がないときは1を補う
if values[0] == list():
    if N != 1:
        values[0].append(1)

#候補がない桁は0を補う
for i in range(N):
    if len(values[i]) == 0:
        values[i].append(0)

#探索
for i in range(1000):
    i = [int(j) for j in list(str(i))]
    if len(i) != N:
        continue
    if N==3:
        if i[0] in values[0] and i[1] in values[1] and i[2] in values[2]:
            print("".join([str(j) for j in i]))
            exit()
    elif N==2:
        if i[0] in values[0] and i[1] in values[1]:
            print("".join([str(j) for j in i]))
            exit()
    else:
        if i[0] in values[0]:
            print("".join([str(j) for j in i]))
            exit()
print(-1)

Buggy submission (verdict: Runtime Error):
N,M = map(int,input().split())
values = []
for i in range(N):
    values.append([])
for i in range(M):
    s,c = map(int,input().split())
    values[s-1].append(c)
for i in range(3):
    if values[i] == list():
        values[i].append(0)
for i in range(1000):
    i = [int(j) for j in list(str(i))]
    if len(i) != N:
        continue
    if N==3:
        if i[0] in values[0] and i[1] in values[1] and i[2] in values[2]:
            print("".join([str(j) for j in i]))
            exit()
    elif N==2:
        if i[0] in values[0] and i[1] in values[1]:
            print("".join([str(j) for j in i]))
            exit()
    else:
        if i[0] in values[0]:
            print("".join([str(j) for j in i]))
            exit()
print(-1)

Buggy submission (verdict: Runtime Error):
N,M = map(int,input().split())
ichi = []
juu = []
hyaku = []
for i in range(M):
    if N == 1:
        s,c = map(int,input().split())
        ichi.append(c)
    elif N == 2:
        s,c = map(int,input().split())
        if s == 1:
            juu.append(c)
        else:
            ichi.append(c)
    elif N == 3:
        s,c = map(int,input().split())
        if s == 1:
            hyaku.append(c)
        elif s == 2:
            juu.append(c)
        else:
            ichi.append(c)
ichi = set(ichi)
juu = set(juu)
hyaku = set(hyaku)
if N == 1:
    if len(ichi) >= 2:
        print(-1)
    else:
        print(min(ichi))
elif N == 2:
    if juu == set([0]):
        print(-1)
    elif len(juu) >= 2:
        print(-1)
    elif len(ichi) >= 2:
        print(-1)
    else:
        if ichi == set():
            ichi.add(0)
        if juu == set():
            juu.add(1)
        print(str(min(juu))+str(min(ichi)))
else:
    if hyaku == set([0]):
        print(-1)
    elif len(hyaku) >= 2:
        print(-1)
    elif len(juu) >= 2:
        print(-1)
    elif len(ichi) >= 2:
        print(-1)
    else:
        if hyaku == set():
            hyaku.add(1)
        if juu == set():
            juu.add(0)
        if ichi == set():
            ichi.add(0)
        print(str(min(hyaku))+str(min(juu))+str(min(ichi)))

Buggy submission (verdict: Runtime Error):
N,M = map(int,input().split())
ichi = []
juu = []
hyaku = []
for i in range(M):
    if N == 1:
        s,c = map(int,input().split())
        ichi.append(c)
    elif N == 2:
        s,c = map(int,input().split())
        if s == 1:
            juu.append(c)
        else:
            ichi.append(c)
    elif N == 3:
        s,c = map(int,input().split())
        if s == 1:
            hyaku.append(c)
        elif s == 2:
            juu.append(c)
        else:
            ichi.append(c)
ichi = set(ichi)
juu = set(juu)
hyaku = set(hyaku)
if N == 1:
    if len(ichi) >= 2:
        print(-1)
    else:
        print(min(ichi))
elif N == 2:
    if juu == set([0]):
        print(-1)
    elif len(juu) >= 2:
        print(-1)
    elif len(ichi) >= 2:
        print(-1)
    else:
        if ichi == set():
            ichi.add(0)
        print(str(min(juu))+str(min(ichi)))
else:
    if hyaku == set([0]):
        print(-1)
    elif len(hyaku) >= 2:
        print(-1)
    elif len(juu) >= 2:
        print(-1)
    elif len(ichi) >= 2:
        print(-1)
    else:
        if juu == set():
            juu.add(0)
        if ichi == set():
            ichi.add(0)
        print(str(min(hyaku))+str(min(juu))+str(min(ichi)))