Problem p03221 (python)

Fixed submission:
N, M = map(int,input().split())

class ID:
    def __init__(self,p,y,i):
        self.p = p
        self.y = y
        self.i = i
        self.ID1 = None
        self.ID2 = None

    def ID(self):
        for i in range(1,7):
            if self.ID1 < 10**i:
                self.ID1 = "0"*(6-i) + str(self.ID1)
                break
        for i in range(1,7):
            if int(self.ID2) < 10**i:
                self.ID2 = "0"*(6-i) + str(self.ID2)
                break
        
        return self.ID1 + self.ID2

cities = []
for i in range(0,M):
    P,Y = map(int,input().split())
    cities.append(ID(P,Y,i))
cities.sort(key = lambda x: x.y)
counter = [-1]

for i in range(0,N):
    counter.append(1)

for c in cities:
    c.ID1 = c.p
    c.ID2 = counter[c.p]
    counter[c.p] = counter[c.p] + 1

cities.sort(key = lambda x: x.i)

for i in range(0,M):
    print(cities[i].ID())

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

class ID:
    def __init__(self,p,y,i):
        self.p = p
        self.y = y
        self.i = i
        self.IDed = False
        self.ID1 = None
        self.ID2 = None

    def ID(self):
        for i in range(1,6):
            if self.ID1 < 10**i:
                self.ID1 = "0"*(6-i) + str(self.ID1)
                break
        for i in range(1,6):
            if int(self.ID2) < 10**i:
                self.ID2 = "0"*(6-i) + str(self.ID2)
                break
        
        return self.ID1 + self.ID2

cities = []
for i in range(0,M):
    P,Y = map(int,input().split())
    cities.append(ID(P,Y,i))
cities.sort(key = lambda x: x.y)
counter = [-1]

for i in range(0,N):
    counter.append(1)

for c in cities:
    c.ID1 = c.p
    c.ID2 = counter[c.p]
    counter[c.p] = counter[c.p] + 1

cities.sort(key = lambda x: x.i)

for i in range(0,M):
    print(cities[i].ID())

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

class ID:
    def __init__(self,p,y,i):
        self.p = p
        self.y = y
        self.i = i
        self.IDed = False
        self.ID1 = None
        self.ID2 = None

    def ID(self):
        for i in range(1,6):
            if self.ID1 < 10**i:
                self.ID1 = "0"*(6-i) + str(self.ID1)
                break
        for i in range(1,6):
            if int(self.ID2) < 10**i:
                self.ID2 = "0"*(6-i) + str(self.ID2)
                break
        
        return self.ID1 + self.ID2

cities = []
for i in range(0,M):
    P,Y = map(int,input().split())
    cities.append(ID(P,Y,i))

for p in range(N+1,1,-1):
    pref = []
    for i in range(0,M):
        if not cities[i].IDed:
            if cities[i].p == p:
                pref.append([cities[i].y, cities[i].i])
                cities[i].IDed = True
    pref.sort()
    count = 1
    for i,j in pref:
        cities[j].ID2 = count
        count = count + 1
        cities[j].ID1 = p

for i in range(0,M):
    print(cities[i].ID())

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

class ID:
    def __init__(self,p,y,i):
        self.p = p
        self.y = y
        self.i = i
        self.IDed = False
        self.ID1 = None
        self.ID2 = None

    def ID(self):
        for i in range(1,6):
            if self.ID1 < 10**i:
                self.ID1 = "0"*(6-i) + str(self.ID1)
                break
        for i in range(1,6):
            if int(self.ID2) < 10**i:
                self.ID2 = "0"*(6-i) + str(self.ID2)
                break
        
        return self.ID1 + self.ID2

cities = []
for i in range(0,M):
    P,Y = map(int,input().split())
    cities.append(ID(P,Y,i))

for p in range(1,N+1):
    pref = []
    for i in range(0,M):
        if not cities[i].IDed:
            if cities[i].p == p:
                pref.append([cities[i].y, cities[i].i])
                cities[i].IDed = True
    pref.sort()
    count = 1
    for i,j in pref:
        cities[j].ID2 = count
        count = count + 1
        cities[j].ID1 = p

for i in range(0,M):
    print(cities[i].ID())