Problem p03221 (python)

Fixed submission:
n,m=map(int,input().split())
l=[list(map(int,input().split())) for i in range(m)]
s=[]
for i in enumerate(l):
    s.append(i)
s=sorted(s,key=lambda x:(x[1][0],x[1][1]))
a=0
for i in range(m):
    if s[i][1][0]==0:
       a=1     
    elif s[i][1][0]==s[i-1][1][0]:
        a+=1
    elif s[i][1][0]!=s[i-1][1][0]:
        a=1
    s[i][1][1]=a
s = sorted(s,key=lambda x:x[0])
for i in s:
    print('%06d'%i[1][0]+'%06d'%i[1][1])

Buggy submission (verdict: Runtime Error):

N, M = map(int,input().split())
P=[]
Y=[]
cities=[]
for i in range(M):
    try:
        s = input()
        p,y = s.split()
        city=(p,y)
        cities.append(city) 
        P.append(p)
        Y.append(y)

    except:
        break
sort_city=sorted(cities,key=lambda x:(x[0],x[1]))
prefs = sorted(set(P))

address=[]
res=[]
for i in range(len(sort_city)): 
    address=str(sort_city[i][0]).rjust(6,'0')+str(i+1).rjust(6,'0')
    res.append((sort_city[i],address))

print(res[1][0])
for i in range(len(cities)):
    for j in range(len(res)):
        if cities[i] == res[j][0]:
            print(res[j][1])

Buggy submission (verdict: Runtime Error):
#M num of prefecture
#N num of city
N, M = map(int,input().split())
#P pref
#Y year
P=[]
Y=[]
cities=[]
for i in range(M):
    try:
        s = input()
        p,y = s.split()
        city=(p,y)
        cities.append(city) 
        P.append(p)
        Y.append(y)
    except:
        break

sort_city=sorted(cities,key=lambda x:(x[0],x[1]))
prefs = sorted(set(P))

address=[]
res=[]
for i in range(len(sort_city)): 
    address=str(sort_city[i][0]).rjust(6,'0')+str(i+1).rjust(6,'0')
    res.append((sort_city[i],address))

print(res[1][0])
for i in range(len(cities)):
    for j in range(len(res)):
        if cities[i] == res[j][0]:
            print(res[j][1])
            break

Buggy submission (verdict: Runtime Error):
#M num of prefecture
#N num of city
N, M = map(int,input().split())
#P pref
#Y year
P=[]
Y=[]
cities=[]
for i in range(M):
    try:
        s = input()
        p,y = s.split()
        city=(p,y)
        cities.append(city) 
        P.append(p)
        Y.append(y)
    except:
        break

sort_city=sorted(cities,key=lambda x:(x[0],x[1]))
prefs = sorted(set(P))

address=[]
res=[]
for i in range(len(sort_city)): 
    address=str(sort_city[i][0]).rjust(6,'0')+str(i+1).rjust(6,'0')
    res.append((sort_city[i],address))

print(res[1][0])
for i in range(len(cities)):
    for j in range(len(res)):
        if cities[i] == res[j][0]:
            print(res[j][1])

Buggy submission (verdict: Runtime Error):
#M num of prefecture
#N num of city
N, M = map(int,input().split())
#P pref
#Y year
P=[]
Y=[]
cities=[]
for i in range(M):
    try:
        s = input()
        p,y = s.split()
        city=(p,y)
        cities.append(city) 
        P.append(p)
        Y.append(y)
    except:
        break

sort_city=sorted(cities,key=lambda x:(x[0],x[1]))
prefs = sorted(set(P))

address=[]
res=[]
for i in range(len(sort_city)):
    pref_n =prefs.index(sort_city[i][0])
    address=str(pref_n+1).rjust(6,'0')+str(i+1).rjust(6,'0')
    res.append((sort_city[i],address))

print(res[1][0])
for i in range(len(cities)):
    for j in range(len(res)):
        if cities[i] == res[j][0]:
            print(res[j][1])