Buggy python submission (verdict: Time Limit Exceeded):
x,y,num = map(int, input().split())
gaiz = []
zen_gaiz = [[] for _ in range(x)]
kaisu = x
for _ in range(num):
    xg,yg = map(int,input().split())
    if xg >= yg:
        zen_gaiz[xg-1].append(yg)
        if xg != yg:
            gaiz.append((xg,yg))

if len(gaiz) ==0:
    print(kaisu)
else:
    finish = False
    for gai in sorted(gaiz, key=lambda x: x[0]):
        hidari = [gai[0]-1, gai[1]]
        while True:
            if hidari[1]==1: 
                kaisu = gai[0]-1
                finish=True
                break
            for g in zen_gaiz:
                if hidari[1] in zen_gaiz[hidari[0]-1]:
                    break
            else:
                hidari[0]-=1
                hidari[1]-=1
                continue
            break
        if finish:
            break
    print(kaisu) 
                

Fixed submission:
x,y,num = map(int, input().split())
gaiz = [x]*x
kaisu = x
for _ in range(num):
    xg,yg = map(int,input().split())
    if xg >= yg:
        sa = xg-yg
        if yg < gaiz[sa]:
            gaiz[sa]=(yg)
 
if len(gaiz) ==0:
    print(kaisu)
else:
    for num,(maegai,gai) in enumerate(zip(gaiz[:-1],gaiz[1:])):
        if maegai>gai and num+gai < kaisu:
            kaisu = num+gai
    print(kaisu)

Buggy python submission (verdict: Time Limit Exceeded):
x,y,num = map(int, input().split())
gaiz = []
zen_gaiz = [[] for _ in range(x)]
kaisu = x
for _ in range(num):
    xg,yg = map(int,input().split())
    zen_gaiz[xg-1].append(yg)
    if xg > yg:
        gaiz.append((xg,yg))

if len(gaiz) ==0:
    print(kaisu)
else:
    finish = False
    for gai in sorted(gaiz, key=lambda x: x[0]):
        hidari = [gai[0]-1, gai[1]]
        while True:
            if hidari[1]==1: 
                kaisu = gai[0]-1
                finish=True
                break
            for g in zen_gaiz:
                if hidari[1] in zen_gaiz[hidari[0]-1]:
                    break
            else:
                hidari[0]-=1
                hidari[1]-=1
                continue
            break
        if finish:
            break
    print(kaisu) 
                

Fixed submission:
x,y,num = map(int, input().split())
gaiz = [x]*x
kaisu = x
for _ in range(num):
    xg,yg = map(int,input().split())
    if xg >= yg:
        sa = xg-yg
        if yg < gaiz[sa]:
            gaiz[sa]=(yg)
 
if len(gaiz) ==0:
    print(kaisu)
else:
    for num,(maegai,gai) in enumerate(zip(gaiz[:-1],gaiz[1:])):
        if maegai>gai and num+gai < kaisu:
            kaisu = num+gai
    print(kaisu)

Buggy python submission (verdict: Time Limit Exceeded):
x,y,num = map(int, input().split())
gaiz = []
zen_gaiz = []
kaisu = x
for _ in range(num):
    xg,yg = map(int,input().split())
    zen_gaiz.append((xg,yg))
    if xg > yg:
        gaiz.append((xg,yg))

if len(gaiz) ==0:
    print(kaisu)
else:
    finish = False
    for gai in sorted(gaiz, key=lambda x: x[0]):
        hidari = [gai[0]-1, gai[1]]
        while True:
            if hidari[1]==1: 
                kaisu = gai[0]-1
                finish=True
                break
            for g in zen_gaiz:
                if g[0]==hidari[0] and g[1]==hidari[1]:
                    break
            else:
                hidari[0]-=1
                hidari[1]-=1
                continue
            break
        if finish:
            break
    print(kaisu) 
                

Fixed submission:
x,y,num = map(int, input().split())
gaiz = [x]*x
kaisu = x
for _ in range(num):
    xg,yg = map(int,input().split())
    if xg >= yg:
        sa = xg-yg
        if yg < gaiz[sa]:
            gaiz[sa]=(yg)
 
if len(gaiz) ==0:
    print(kaisu)
else:
    for num,(maegai,gai) in enumerate(zip(gaiz[:-1],gaiz[1:])):
        if maegai>gai and num+gai < kaisu:
            kaisu = num+gai
    print(kaisu)