Problem p02761 (python)

Fixed submission:
# -*- coding: utf-8 -*-
def main():
    import sys
    input = sys.stdin.readline
    N,M = map(int,input().split())
    SC = [list(map(int,input().split())) for i in range(M)]
    ans = ["x","x","x"]
    if N == 1:
        ans[0] = "0"
        ans[1] = "0"
        for sc in SC:
            if sc[0] == 1:
                if ans[2] == "x":
                    ans[2] = str(sc[1])
                elif ans[2] != "x" and ans[2] != str(sc[1]):
                    print(-1)
                    exit()
            elif sc[0] == 2 or sc[0] == 3:
                print(-1)
                exit()
    elif N == 2:
        ans[0] = "0"
        for sc in SC:
            if sc[0] == 1:
                if ans[1] == "x":
                    ans[1] = str(sc[1])
                elif ans[1] != "x" and ans[1] != str(sc[1]):
                    print(-1)
                    exit()
            elif sc[0] == 2:
                if ans[2] == "x":
                    ans[2] = str(sc[1])
                elif ans[2] != "x" and ans[2] != str(sc[1]):
                    print(-1)
                    exit()
            elif sc[0] == 3:
                print(-1)
                exit()
    elif N == 3:
        for sc in SC:
            if sc[0] == 1:
                if ans[0] == "x":
                    ans[0] = str(sc[1])
                elif ans[0] != "x" and ans[0] != str(sc[1]):
                    print(-1)
                    exit()
            elif sc[0] == 2:
                if ans[1] == "x":
                    ans[1] = str(sc[1])
                elif ans[1] != "x" and ans[1] != str(sc[1]):
                    print(-1)
                    exit()
            elif sc[0] == 3:
                if ans[2] == "x":
                    ans[2] = str(sc[1])
                elif ans[2] != "x" and ans[2] != str(sc[1]):
                    print(-1)
                    exit()
    if N == 1:
        if ans[0] == "x":
            ans[1] = "0"
        if ans[1] == "x":
            ans[1] = "0"
        if ans[2] == "x":
            ans[2] = "0"
        print(int("".join(ans)))
    elif N == 2:
        if ans[1] == "0":
            print(-1)
            exit()
        if ans[0] == "x":
            ans[1] = "0"
        if ans[1] == "x":
            ans[1] = "1"
        if ans[2] == "x":
            ans[2] = "0"
        print(int("".join(ans)))
    elif N == 3:
        if ans[0] == "0":
            print(-1)
            exit()
        if ans[0] == "x":
            ans[0] = "1"
        if ans[1] == "x":
            ans[1] = "0"
        if ans[2] == "x":
            ans[2] = "0"
        print(int("".join(ans)))

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
def main():
    import sys
    input = sys.stdin.readline
    N,M = map(int,input().split())
    SC = [list(map(int,input().split())) for i in range(M)]
    ans = ["x","x","x"]
    if N == 1:
        ans[0] = "0"
        ans[1] = "0"
        for sc in SC:
            if sc[0] == 1:
                if ans[2] == "x":
                    ans[2] = str(sc[1])
                elif ans[2] != "x" and ans[2] != str(sc[1]):
                    print(-1)
                    exit()
            elif sc[0] == 2 or sc[0] == 3:
                print(-1)
                exit()
    elif N == 2:
        ans[0] = "0"
        for sc in SC:
            if sc[0] == 1:
                if ans[1] == "x":
                    ans[1] = str(sc[1])
                elif ans[1] != "x" and ans[1] != str(sc[1]):
                    print(-1)
                    exit()
            elif sc[0] == 2:
                if ans[2] == "x":
                    ans[2] = str(sc[1])
                elif ans[2] != "x" and ans[2] != str(sc[1]):
                    print(-1)
                    exit()
            elif sc[0] == 3:
                print(-1)
                exit()
    elif N == 3:
        for sc in SC:
            if sc[0] == 1:
                if ans[0] == "x":
                    ans[0] = str(sc[1])
                elif ans[0] != "x" and ans[0] != str(sc[1]):
                    print(-1)
                    exit()
            elif sc[0] == 2:
                if ans[1] == "x":
                    ans[1] = str(sc[1])
                elif ans[1] != "x" and ans[1] != str(sc[1]):
                    print(-1)
                    exit()
            elif sc[0] == 3:
                if ans[2] == "x":
                    ans[2] = str(sc[1])
                elif ans[2] != "x" and ans[2] != str(sc[1]):
                    print(-1)
                    exit()
    if N == 1:
        print(int("".join(ans)))
    elif N == 2:
        if ans[1] == "0":
            print(-1)
            exit()
        if ans[1] == "x":
            ans[1] = "1"
        if ans[2] == "x":
            ans[2] = "0"
        print(int("".join(ans)))
    elif N == 3:
        if ans[0] == "0":
            print(-1)
            exit()
        if ans[0] == "x":
            ans[0] = "1"
        if ans[1] == "x":
            ans[1] = "0"
        if ans[2] == "x":
            ans[2] = "0"
        print(int("".join(ans)))

if __name__ == "__main__":
    main()