Problem p02761 (python)

Fixed submission:
N,M = map(int,input().split())
if M==0:
  if N==1:
    print(0)
    exit()
  elif N==2:
    print(10)
    exit()
  else:
    print(100)
    exit()
SC = [list(map(int,input().split())) for _ in range(M)]
if N==1:
  s,c = SC[0][0],SC[0][1]
  for sc in SC:
    if c==sc[1]:
      continue
    else:
      print(-1)
      exit()
  print(c)
  
else:
  lis = [[] for _ in range(N)]
  for sc in SC:
    lis[sc[0]-1].append(sc[1])
    
  if 0 in lis[0]:
    print(-1)
    exit()
    
  else:
    if N==2:
      if lis[0]==[]:
        a = str(1)
      else:
        a = str(lis[0][0])
        
      if lis[1]==[]:
        b = str(0)
      else:
        b = str(lis[1][0])
        
      for i in lis[0]:
        if a==str(i):
          continue
        else:
          print(-1)
          exit()
      for i in lis[1]:
        if b==str(i):
          continue
        else:
          print(-1)
          exit()
      print(int(a+b))
      exit()
      
    else:
      if lis[0]==[]:
        a = str(1)
      else:
        a = str(lis[0][0])
        
      if lis[1]==[]:
        b = str(0)
      else:
        b = str(lis[1][0])
        
      if lis[2]==[]:
        c=str(0)
      else:
        c = str(lis[2][0])
      for i in lis[0]:
        if a==str(i):
          continue
        else:
          print(-1)
          exit()
      for i in lis[1]:
        if b==str(i):
          continue
        else:
          print(-1)
          exit()
      for i in lis[2]:
        if c==str(i):
          continue
        else:
          print(-1)
          exit()
      print(int(a+b+c))
      exit()

Buggy submission (verdict: Runtime Error):
N,M = map(int,input().split())
if M==0:
  if N==1:
    print(0)
  elif N==2:
    print(10)
  else:
    print(100)
SC = [list(map(int,input().split())) for _ in range(M)]
if N==1:
  s,c = SC[0][0],SC[0][1]
  for sc in SC:
    if c==sc[1]:
      continue
    else:
      print(-1)
      exit()
  print(c)
  
else:
  lis = [[] for _ in range(N)]
  for sc in SC:
    lis[sc[0]-1].append(sc[1])
    
  if 0 in lis[0]:
    print(-1)
    exit()
    
  else:
    if N==2:
      if lis[0]==[]:
        a = str(1)
      else:
        a = str(lis[0][0])
        
      if lis[1]==[]:
        b = str(0)
      else:
        b = str(lis[1][0])
        
      for i in lis[0]:
        if a==str(i):
          continue
        else:
          print(-1)
          exit()
      for i in lis[1]:
        if b==str(i):
          continue
        else:
          print(-1)
          exit()
      print(int(a+b))
      exit()
      
    else:
      if lis[0]==[]:
        a = str(1)
      else:
        a = str(lis[0][0])
        
      if lis[1]==[]:
        b = str(0)
      else:
        b = str(lis[1][0])
        
      if lis[2]==[]:
        c=str(0)
      else:
        c = str(lis[2][0])
      for i in lis[0]:
        if a==str(i):
          continue
        else:
          print(-1)
          exit()
      for i in lis[1]:
        if b==str(i):
          continue
        else:
          print(-1)
          exit()
      for i in lis[2]:
        if c==str(i):
          continue
        else:
          print(-1)
          exit()
      print(int(a+b+c))
      exit()