In this game, Takahashi and Aoki play on a grid with H rows and W columns. There are N obstacles at specific coordinates. Players take turns moving a piece to an adjacent cell or not moving it. The game ends when the piece doesn't move twice in a row. Takahashi aims to perform as many actions as possible before the game ends, while Aoki aims to perform as few actions as possible. The task is to determine the number of actions Takahashi will perform. The constraints are 1 ≤ H,W ≤ 2*10^5, 0 ≤ N ≤ 2*10^5, 1 ≤ X_i ≤ H, 1 ≤ Y_i ≤ W, and other conditions. The input format is H, W, N, followed by N lines of X and Y coordinates. The output is the number of actions Takahashi will perform. Sample inputs and outputs are provided.

# Buggy version:
h,w,n=map(int,input().split())
xy=[list(map(int,input().split())) for i in range(n)]
xy.sort()
cnt=0
ans=h
for i in range(n):
  if x-1>=y+cnt:
    ans=x-1
    break
  elif x==y+cnt:
    cnt+=1
print(ans)


# Fixed version:
h,w,n=map(int,input().split())
xy=[list(map(int,input().split())) for i in range(n)]
xy.sort()
cnt=0
ans=h
for x,y in xy:
  if x-1>=y+cnt:
    ans=x-1
    break
  elif x==y+cnt:
    cnt+=1
print(ans)

# Buggy version:
H, W, N = map(int, input().split())
xy = []
for i in range(N):
    x, y = map(int, input().split())
    xy.append((x, y))
xy.sort()

ans = W
d = 0
for x, y in xy:
    if x-d>y:
        print(x, y)
        ans = x-1
        break
    if x-d == y:
        d+=1
print(ans)






# Fixed version:
H, W, N = map(int, input().split())
xy = []
for i in range(N):
    x, y = map(int, input().split())
    xy.append((x, y))
xy.sort()

ans = H
d = 0
for x, y in xy:
    if x-d>y:
        ans = x-1
        break
    if x-d == y:
        d+=1
print(ans)

# Buggy version:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(pow(10, 6))

def main():
    h, w, n = map(int, input().split())
    xy = [tuple(map(int, input().split())) for _ in range(n)]
    miny = [float("inf") for _ in range(h + 1)]
    for x, y in xy:
        miny[x - 1] = min(miny[x - 1], y - 1)
    miny[h] = 0
    
    tmpx, tmpy = 0, 0
    while tmpx < h:
        if miny[tmpx + 1] == tmpy + 1:
            tmpx += 1
        elif miny[tmpy + 1] < tmpy:
            break
        else:
            tmpx += 1
            tmpy += 1
    
    print(tmpx + 1)


if __name__ == '__main__':
    main()


# Fixed version:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(pow(10, 6))

def main():
    h, w, n = map(int, input().split())
    xy = [tuple(map(int, input().split())) for _ in range(n)]
    miny = [float("inf") for _ in range(h + 1)]
    for x, y in xy:
        miny[x - 1] = min(miny[x - 1], y - 1)
    miny[h] = 0
    
    tmpx, tmpy = 0, 0
    while tmpx < h:
        if miny[tmpx + 1] <= tmpy:
            break
        elif miny[tmpx + 1] == tmpy + 1:
            tmpx += 1
        else:
            tmpx += 1
            tmpy += 1
    
    print(min(tmpx + 1, h))


if __name__ == '__main__':
    main()

# Buggy version:
H, W, N = map(int, input().split())
posObss = [{H} for y in range(W)]
for _ in range(N):
    X, Y = map(int, input().split())
    posObss[Y - 1].add(X - 1)

ans = H
xNow = 0
for y in range(W):
    xLim = min([x for x in posObss[y] if x > xNow])
    ans = min(ans, xLim)

    xNow += 1
    while xNow in posObss[y + 1]:
        xNow += 1
    if xNow >= xLim: break

print(ans)


# Fixed version:
H, W, N = map(int, input().split())
posObss = [{H} for y in range(W)]
for _ in range(N):
    X, Y = map(int, input().split())
    posObss[Y - 1].add(X - 1)

ans = H
xNow = 0
for y in range(W):
    xLim = min([x for x in posObss[y] if x > xNow])
    ans = min(ans, xLim)

    if y == W - 1: break
    xNow += 1
    while xNow in posObss[y + 1]:
        xNow += 1
    if xNow >= xLim: break

print(ans)

# Buggy version:
from collections import defaultdict
import sys
def inpl(): return [int(i) for i in input().split()]
H, W, N = inpl()
if not N:
    print(W)
    sys.exit()
A = []
B = defaultdict(lambda: [])
C = defaultdict(lambda: W)
for _ in range(N):
    x, y = inpl()
    A.append((x,y))
    B[y].append(x)
T = [0]*(H+1)
T[1] = 2
for i in range(1,H):
    if not B[i+1]:
        T[i+1] = min(W,T[i] + 1)
        continue
    ctr = T[i]
    while True:
        if ctr in B[i+1]:
            ctr += 1
            continue
        break
    T[i+1] = min(ctr+1, W)
for x,y in A:
    if x >= T[y]:
        C[y] = min(C[y], x)
print(min([i-1 for i in C.values()]+[W]))
print(T)

# Fixed version:
from collections import defaultdict
import sys
def inpl(): return [int(i) for i in input().split()]
H, W, N = inpl()
if not N:
    print(H)
    sys.exit()
A = []
B = defaultdict(lambda: [])
C = defaultdict(lambda: H+1)
for _ in range(N):
    x, y = inpl()
    A.append((x,y))
    B[y].append(x)
T = [0]*(W+1)
T[1] = 2
for i in range(1,W):
    if not B[i+1]:
        T[i+1] = T[i] + 1
        continue
    ctr = T[i]
    while True:
        if ctr in B[i+1]:
            ctr += 1
            continue
        break
    T[i+1] = min(ctr+1, H+1)
for x,y in A:
    if x >= T[y]:
        C[y] = min(C[y], x)
print(min([i-1 for i in C.values()]+[H]))

# Buggy version:
H,W,N = map(int,input().split())
XY = []
for i in range(N):
    x,y = map(int,input().split())
    XY.append((x,y))

XY.sort()

ans,d = 9999999999999,0
for xy in XY:
    x,y = xy[0],xy[1]
    if x == y+d:
        d += 1
    elif x > y+d:
        ans = min(ans, x-1)

print(ans)

# Fixed version:
H,W,N = map(int,input().split())
XY = []
for i in range(N):
    x,y = map(int,input().split())
    XY.append((x,y))

XY.sort()

ans,d = H,0
for xy in XY:
    x,y = xy[0],xy[1]
    if x == y+d:
        d += 1
    elif x > y+d:
        ans = min(ans, x-1)

print(ans)

# Buggy version:
import bisect
h,w,n = map(int,input().split())
a = []
for i in range(n):
    x,y = map(int,input().split())
    x-=1
    y-=1
    a.append([x,y])
kabe = [[] for i in range(w)]
for i,j in a:
    kabe[j].append(i)
for i in range(w):
    kabe[i].sort()
ans = float("inf")
to = 0
for i in range(w):
    mo = bisect.bisect_right(kabe[i], to)
    if mo < len(kabe[i]):
        ans = min(ans,kabe[i][mo])
    else:
        ans = min(ans,h+1)
    if i == w-1:
        break
    c = set(kabe[i+1])
    to += 1
    while to in c:
        to+=1
print(ans)

# Fixed version:
import bisect
h,w,n = map(int,input().split())
a = []
for i in range(n):
    x,y = map(int,input().split())
    x-=1
    y-=1
    a.append([x,y])
kabe = [[] for i in range(w)]
for i,j in a:
    kabe[j].append(i)
for i in range(w):
    kabe[i].sort()
ans = float("inf")
to = 0
for i in range(w):
    mo = bisect.bisect_right(kabe[i], to)
    if mo < len(kabe[i]):
        ans = min(ans,kabe[i][mo])
    else:
        ans = min(ans,h)
    if i == w-1:
        break
    c = set(kabe[i+1])
    to += 1
    while to in c:
        to+=1
    if to >h:
        break
print(ans)