Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

import sys


n,k= map(int,input().split())
v={}

min = sys.maxsize

if n == 0 :print(0);exit()
if n >= 10**18 :print(0);exit()

while True:
    diff = abs(n- k)

    if v.get(diff):
        break
    else:
        v[diff] = True
        n = diff

        if diff < min:
            min = diff



print(min)

### FIXED CODE ###

import sys

n,k= map(int,input().split())
v={}

min = sys.maxsize

if n == 0 :print(0);exit()

if n > k :
    x = n%k
    if x == 0 or x > k: 
        print(x);exit()
    else:
        n=x
        v[x] = True

while True:
    diff = abs(n- k)

    if v.get(diff):
        break
    else:
        # print(diff)
        v[diff] = True
        n = diff

        if diff < min:
            min = diff



print(min)
### BUGGY CODE ###

N, P = map(int, input().split())
S = input()

if P == 2 or P == 5:
    ans = 0
    for i, s in enumerate(S):
        if int(s) % P == 0:
            ans += i+1
    print(ans)
    import sys
    sys.exit(0)

suf_nums = [-1] * N
s = 0
power = 1
for i in range(N):
    s += int(S[-1-i]) * power
    suf_nums[i] = s
    power *= 10

surpluses = [0] * P
ans = 0
for num in suf_nums:
    surplus = num % P
    surpluses[surplus] += 1
    if surplus == 0:
        ans += surpluses[0]
    else:
        ans += surpluses[surplus] - 1

print(ans)


### FIXED CODE ###

N, P = map(int, input().split())
S = input()

if P == 2 or P == 5:
    ans = 0
    for i, s in enumerate(S):
        if int(s) % P == 0:
            ans += i+1
    print(ans)
    import sys
    sys.exit(0)

surpluses = [0] * P
s = 0
power = 1
for i in range(N):
    s = (s + int(S[-1-i]) * power) % P
    surpluses[s % P] += 1
    power = (power * 10) % P

ans = surpluses[0]
for count in surpluses:
    ans += count * (count - 1) // 2

print(ans)
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**6)

N, K = map(int, input().split())
h = list(map(int, input().split()))
INF = 10**9
dp = [INF] * N

def solve(n):
    if dp[n] < INF:
        return dp[n]

    if n == 0:
        dp[n] = 0
    elif n == 1:
        dp[n] = abs(h[0] - h[1])
    else:
        for i in range(1, K+1):
            if n - i < 0:
                break
            if dp[n - i] < INF:
                cost = dp[n - i] + abs(h[n - i] - h[n])
            else:
                cost = solve(n - i) + abs(h[n - i] - h[n])
            dp[n] = min(dp[n], cost)

    return dp[n]

print(solve(N-1))

### FIXED CODE ###

n, k = map(int, input().split())
h = list(map(int, input().split()))
dp = [0] * n

for i in range(n):
    if i == 0:
        dp[0] = 0
    elif i == 1:
        dp[1] = abs(h[0]-h[1])
    else:
        costs = []
        for j in range(1, k+1):
            if j > i:
                break
            if abs(h[i-j]-h[i]) == 0:
                costs.append(dp[i-j])
                break
            costs.append(dp[i-j] + abs(h[i-j]-h[i]))

        # if i == 3:
        #     print(costs)
        dp[i] = min(costs)

#print(dp)
print(dp[n-1])
### BUGGY CODE ###

def main(H,W,ch,cw,dh,dw,S):
    q=np.zeros((3*10**7,3),dtype=np.int32)
    v=-np.ones((H,W),dtype=np.int32)
    x=[[1,0],[-1,0],[0,-1],[0,1]]
    y=[[2,2],[2,1],[2,0],[2,-1],[2,-2],[1,2],[1,1],[1,-1],[1,-2],[0,2],[0,-2],[-1,2],[-1,1],[-1,-1],[-1,-2],[-2,2],[-2,1],[-2,0],[-2,-1],[-2,-2]]
    l,r=5*10**6,5*10**6+1
    q[l][0],q[l][1],q[l][2]=ch-1,cw-1,0
    while l<r:
        h,w,d=q[l][0],q[l][1],q[l][2]
        l+=1
        if v[h][w]<0:
            v[h][w]=d
        for k in range(4):
            i,j=x[k][0],x[k][1]
            if 0<=h+i<H and 0<=w+j<W and S[h+i][w+j] and v[h+i][w+j]<0:
                l-=1
                q[l][0],q[l][1],q[l][2]=h+i,w+j,d
        for k in range(20):
            i,j=y[k][0],y[k][1]
            if 0<=h+i<H and 0<=w+j<W and S[h+i][w+j] and v[h+i][w+j]<0:
                q[r][0],q[r][1],q[r][2]=h+i,w+j,d+1
                r+=1
    return v[dh-1][dw-1]
import sys
import numpy as np
readline=sys.stdin.buffer.readline
read=sys.stdin.buffer.read
if sys.argv[-1]=='ONLINE_JUDGE':
    from numba import*
    from numba.pycc import CC
    cc=CC('my_module')
    def cc_export(f,s):
        cc.export(f.__name__,s)(f)
        return njit(f)
    main=cc_export(main,(i4,i4,i4,i4,i4,i4,i4[:,:]))
    cc.compile()
    exit()
H,W=map(int,readline().split())
ch,cw=map(int,readline().split())
dh,dw=map(int,readline().split())
S=np.array([[1 if c==ord('.') else 0 for c in readline().strip()] for i in range(H)],dtype=np.int32)
from my_module import main
print(main(H,W,ch,cw,dh,dw,S))


### FIXED CODE ###

H,W=map(int,input().split())
ch,cw=map(int,input().split())
dh,dw=map(int,input().split())
S=[input() for i in range(H)]
from collections import deque
q=deque([(ch-1,cw-1,0)])
v=[[9**9]*W for i in range(H)]
x=((1,0),(-1,0),(0,-1),(0,1))
y=((2,2),(2,1),(2,0),(2,-1),(2,-2),(1,2),(1,1),(1,-1),(1,-2),(0,2),(0,-2),(-1,2),(-1,1),(-1,-1),(-1,-2),(-2,2),(-2,1),(-2,0),(-2,-1),(-2,-2))
while q:
    h,w,d=q.popleft()
    for i,j in x:
        if 0<=h+i<H and 0<=w+j<W and S[h+i][w+j]=='.' and d<v[h+i][w+j]:
            v[h+i][w+j]=d
            q.appendleft((h+i,w+j,d))
    for i,j in y:
        if 0<=h+i<H and 0<=w+j<W and S[h+i][w+j]=='.' and d+1<v[h+i][w+j]:
            v[h+i][w+j]=d+1
            q.append((h+i,w+j,d+1))
print(v[dh-1][dw-1] if v[dh-1][dw-1]<9**9 else -1)
### BUGGY CODE ###

from collections import Counter,defaultdict
import sys,heapq,bisect,math,itertools,string,queue,datetime
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

s = input()[::-1]
rem = [0] * 13
rem[0] = 1
for i, st in enumerate(s):
    if st == '?':
        tmp = rem[:]
        rem = [0] * 13
        for j in range(10):
            next = (j * 10 ** i) % 13
            for k in range(13):
                rem[(k+next)%13] += tmp[k]
    else:
        next = (int(st) * 10**i) % 13
        tmp = rem[:]
        for j in range(13):
            rem[(j+next)%13] = tmp[j]
    for _ in rem:
        _ %= mod
print(rem[5] % mod)

### FIXED CODE ###

from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

s = sys.stdin.readline()
n = len(s)-1
now = [0] * 13
now[0] = 1
for i in range(n)[::-1]:
    prev = now[::]
    now = [0] * 13
    c = n - i - 1#桁
    m = pow(10,c,13)
    if s[i] == '?':
        for k in range(10):
            l = (m*k) % 13
            for j in range(13):
                now[(j+l)%13] += prev[j]
                now[(j+l)%13] = now[(j+l)%13] % mod
    else:
        k = int(s[i])
        l = (m*k) % 13
        for j in range(13):
            now[(j+l)%13] = prev[j]
    # print(now)
print(now[5])
### BUGGY CODE ###



n, m = [ int(v) for v in input().split() ]
num_list = sorted([ int(v) for v in input().split() ])
change_list = []
for i in range(m):
	a,b = [ int(v) for v in input().split() ]
	change_list += [b]*a
change_list = sorted(change_list,reverse = True)

x = len(change_list)
if n >= x:
	change_list += ([0]*(n-x))
else:
	change_list = change_list[:n]


anslist = [ max(num_list[i], change_list[i])for i in range(n)]
print(sum(anslist))


### FIXED CODE ###

import sys
input = sys.stdin.readline

def swap(a):
    return ((a[1],a[0]))


n, m = [ int(v) for v in input().split() ]
num_list = [ (int(v), 1) for v in input().split() ]
change_list = [ swap([ int(v) for v in input().split() ]) for i in range(m) ]
total_list = sorted(num_list + change_list, reverse = True)

k = n
ans = 0

for i in total_list:

    j = min(i[1], k)
    k -= j
    ans += i[0] * j
    if k == 0:
        break
print(ans)
### BUGGY CODE ###

import math
import fractions
n = int(input())
a_ls = list(map(int,input().split()))
lcm = 1
ans = 0
mod = 10**9+7
for i in a_ls:
    lcm = lcm * i//fractions.gcd(lcm,i)
for i in a_ls:
    ans += (lcm//i)
print(ans%mod)


### FIXED CODE ###

import math
import fractions
n = int(input())
a_ls = list(map(int,input().split()))
lcm = 1
ans = 0
mod = 10**9+7
for i in a_ls:
    lcm = lcm * i//fractions.gcd(lcm,i)
for i in a_ls:
    ans += lcm//i
print(ans%mod)
### BUGGY CODE ###

import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

N = int(input())
x, y = map(int, input().split())
s = x + y
for i in range(N - 1):
    t, a = map(int, input().split())
    if s % (t + a) == 0:
        x2 = t * (s // (t + a))
        y2 = a * (s // (t + a))
        while x2 < x or y2 < y:
            x2 += t
            y2 += a
        x, y = x2, y2
    else:
        s += (t + a) - s % (t + a)
        x2 = t * (s // (t + a))
        y2 = a * (s // (t + a))
        while x2 < x or y2 < y:
            x2 += t
            y2 += a
        x, y = x2, y2
    s = x + y
print(s)


### FIXED CODE ###

N = int(input())
a, b = 1, 1
for i in range(N):
    x, y = map(int, input().split())
    n = max((a + x - 1) // x, (b + y - 1) // y)
    a = n * x
    b = n * y
print(a + b)
### BUGGY CODE ###

S=input()
a=0
b=[0]*2019
for i in S:
    l=[None]*2019
    k=int(i)
    for j in range(2019):
        l[(j*10+k)%2019]=b[j]
    l[k]+=1
    a+=l[0]
    b=l
print(a)

### FIXED CODE ###

S=input()
a=0
b=[0]*2019
No=[None]*2019
for i in S:
    l=No.copy()
    k=int(i)
    for j in range(2019):
        l[(j*10+k)%2019]=b[j]
    l[k]+=1
    a+=l[0]
    b=l
print(a)
### BUGGY CODE ###

from fractions import gcd

MOD = 10**9 + 7
N = int(input())
A = list(map(int, input().split()))

l = A[0]
for a in A:
  if l % a != 0:
    l = l * a // gcd(l, a)

ans = 0
for a in A:
  ans += (l//a) % MOD
  ans %= MOD

print(ans)

### FIXED CODE ###

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def lcm(a, b):
    return a * b // gcd (a, b)


def power(n, r, mod=10**9+7):
    if r == 0: return 1
    if r%2 == 0:
        return power(n*n % mod, r//2, mod) % mod
    if r%2 == 1:
        return n * power(n, r-1, mod) % mod


MOD = 10**9 + 7
N = int(input())
A = list(map(int, input().split()))

l = A[0]
for a in A:
  t = l%a
  if t != 0:
    l = lcm(l, a)

l %= MOD
ans = 0
for a in A:
  ans += (l * power(a, MOD-2)) % MOD
  ans %= MOD

print(ans)
### BUGGY CODE ###

from numba import njit

@njit('i8(i8)', cache=True)
def popcount(n):
    a = 0
    while n != 0:
        a += n & 1
        n >>= 1
    return a

@njit('i8(i8)', cache=True)
def fun(n):
    ops = 0
    while n:
        bits = popcount(n)
        n %= bits
        ops += 1
    return ops

def funwrap(n, pc):
    if n == 0:
        return 0
    return 1 + fun(n % pc)

n = int(input())
xs = list(map(int, input()))

from functools import reduce
sm = reduce(lambda x,y: x*2+y, xs)
pc = xs.count(1)

for i in range(n):
    df = 1 if xs[i] ^ 1 == 1 else -1

    dn = 1 << (n - (i + 1))
    smi = sm + dn * df
    pci = pc + df

    print(funwrap(smi, pci))


### FIXED CODE ###

from numba import njit

@njit('i8(i8)', cache=True)
def popcount(n):
    a = 0
    while n != 0:
        a += n & 1
        n >>= 1
    return a

@njit('i8(i8)', cache=True)
def fun(n):
    ops = 0
    while n:
        pc = popcount(n)
        n %= pc
        ops += 1
    return ops

n = int(input())
xs = list(map(int, input()))

from functools import reduce
sm = reduce(lambda x,y: x*2+y, xs)
pc = xs.count(1)

# pcの差分を出した後の剰余計算を速くするため、事前にmod pc+1, mod pc-1を計算しておく
mp = sm % (pc + 1)
mm = sm % (pc - 1) if pc > 1 else 0

for i in range(n):
    df = 1 if xs[i] == 0 else -1
    pci = pc + df
    if pci == 0:
        print(0)
        continue

    rest = pow(2, n - i - 1, pci)

    if xs[i] == 0:
        smi = (mp + rest) % pci
        print(1 + fun(smi))
    else:
        smi = (mm - rest) % pci
        print(1 + fun(smi))
### BUGGY CODE ###

n=int(input())
a=[]
for i in range(n):
    a.append(int(input())-1)

#TLEチーンコ
i=0
j=0
b=[]
while True:
    j+=1
    if a[i]==1:
        print(j)
        break
    elif a[i] in b:
        print(-1)
        break
    else:
        b.append(a[i])
        i=a[i]



### FIXED CODE ###

n=int(input())
a=[]
for i in range(n):
    a.append(int(input())-1)
'''
#TLEチーンコ
i=0
j=0
b=[]
while True:
    j+=1
    if a[i]==1:
        print(j)
        break
    elif a[i] in b:
        print(-1)
        break
    else:
        b.append(a[i])
        i=a[i]
'''
i=0
j=0
while True:
    j+=1
    if a[i]==1:
        print(j)
        break
    elif j==n:
        print(-1)
        break
    else:
        i=a[i]
### BUGGY CODE ###

import numpy as np
 
n = int(input())
a = list(map(int, input().split()))
counter = [0 for _ in range(n)]
for i in range(n - 1):
    a_np = np.roll(np.array(a), i + 1)
    index = list(np.where(a % a_np == 0))
    for n in index[0]:
        counter[int(n)] += 1
print(counter.count(0))

### FIXED CODE ###

import numpy as np
 
n = int(input())
a = list(map(int, input().split()))
a.sort()
dp = [0 for _ in range(a[-1] + 1)]

for x in a:
    for j in range(x, a[-1] + 1, x):
        dp[j] += 1
ans = 0
for x in set(a):
    #print(x)
    if dp[x] == 1:
        ans += 1
print(ans)
### BUGGY CODE ###

x = int(input())
y = input().split()
for u in range(len(y)):
    y[u] = int(y[u])
y = sorted(y)
count = 0

for i in range(max(y)):
    a = [u for u in y if u > i]
    if len(a) == int(len(y) / 2):
        count += 1
print(count)

### FIXED CODE ###

x = int(input())
y = input().split()
for u in range(len(y)):
    y[u] = int(y[u])
y = sorted(y)
count = y[int(len(y) / 2)] - y[int((len(y) / 2)) - 1]
print(count)
### BUGGY CODE ###

import queue
h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
ans = []
q = queue.Queue()

if s[0][0] == '#':
    q.put([0, 0, 1])
else:
    q.put([0, 0, 0])

while not q.empty():
    a = q.get()
    if a[0] == h-1 and a[1] == w-1:
        ans.append(a[2])

    if a[0]+1 < h:
        if s[a[0]+1][a[1]] == '#':
            q.put([a[0]+1, a[1], a[2]+1])
        elif s[a[0]+1][a[1]] != '#':
            q.put([a[0]+1, a[1], a[2]])

    if a[1]+1 < w:
        if s[a[0]][a[1]+1] == '#':
            q.put([a[0], a[1]+1, a[2]+1])
        elif s[a[0]][a[1]+1] != '#':
            q.put([a[0], a[1]+1, a[2]])
print(min(ans))


### FIXED CODE ###

import queue
h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
ans = float('inf')
q = queue.Queue()
vals = [[float('inf')]*w for _ in range(h)]
vals[0][0] = 0
q.put([0, 0])

while not q.empty():
    a = q.get()
    for i, j in ((1, 0), (0, 1)):
        x, y = a[0]+i, a[1]+j
        if 0 <= x < h and 0 <= y < w:
            if s[x][y] == '#' and s[a[0]][a[1]] == '.':
                if vals[x][y] > vals[a[0]][a[1]]+1:
                    vals[x][y] = vals[a[0]][a[1]]+1
                    q.put([x, y])
            elif vals[x][y] > vals[a[0]][a[1]]:
                vals[x][y] = vals[a[0]][a[1]]
                q.put([x, y])
ans = vals[-1][-1]
if s[0][0] == '#':
    ans += 1

print(ans)
### BUGGY CODE ###

import copy
N = int(input())
S = input()

S_set = {S[0:3]}
cnst = 0
for i in range(3,N):
    S_0_set = S_set.copy()
    for s_0 in S_0_set:
        for j in range(len(s_0)):
            if s_0[0:j]+s_0[j+1:3]+S[i] not in S_set:
                S_set.add(s_0[0:j]+s_0[j+1:3]+S[i])
    if len(S_set) == 1000:
        print(1000)
        cnst = 1
        break
if cnst == 0:
    print(len(S_set))


### FIXED CODE ###

N = int(input())
S = input()

def judge(S,A): #A:3桁ｎ数字
    dig,pt = 0,0
    while dig < 3 and pt < N:
        if S[pt] == A[dig]:
            dig += 1
            pt += 1
        else:
            pt += 1
        if dig == 3:
            break

    if dig == 3:
        return True
    else:
        return False

count = 0
for a in range(10):
    for b in range(10):
        for c in range(10):
            if judge(S,[str(a),str(b),str(c)]):
                count += 1

print(count)
### BUGGY CODE ###

from collections import deque

N, X, Y = map(int, input().split())
X, Y = X - 1, Y - 1
ans = [0] * N
for sv in range(N):
    dist = [float("inf")] * N
    q = deque([sv])

    def push(v, d):
        if dist[v] != float("inf"):
            return
        dist[v] = d
        q.append(v)

    push(sv, 0)
    while len(q):
        v = q.popleft()
        d = dist[v]
        if v - 1 >= 0:
            push(v - 1, d + 1)
        if v + 1 < N:
            push(v + 1, d + 1)
        if v == X:
            push(Y, d + 1)
        if v == Y:
            push(X, d + 1)
    for i in range(N):
        ans[dist[i]] += 1
for i in range(N):
    ans[i] //= 2
for i in range(1, N):
    print(ans[i])

### FIXED CODE ###

from collections import defaultdict

N, X, Y = map(int, input().split())
X, Y = X - 1, Y - 1
dist_to_cnt = defaultdict(int)
for i in range(N):
    for j in range(i, N):
        dist = min(j - i, abs(i - X) + 1 + abs(Y - j))
        dist_to_cnt[dist] += 1
for i in range(1, N):
    print(dist_to_cnt[i])
### BUGGY CODE ###

N=int(input())
alist=list(map(int,input().split()))

def bs_left(mid,mp):
  sum2=abs(2*slist[mid]-slist[mp])
  if mid==0:
    sum1=sum2
  else:
    sum1=abs(2*slist[mid-1]-slist[mp])
  if mid==mp:
    sum3=sum2
  else:
    sum3=abs(2*slist[mid+1]-slist[mp])

  if sum2<=sum1 and sum2<=sum3:
    return 0
  elif sum1<sum2:
    return -1
  elif sum2>sum3:
    return 1    

def bs_right(mid,mp):
  sum2=abs(2*(slist[mid]-slist[mp])-(slist[-1]-slist[mp]))
  if mid==0:
    sum1=sum2
  else:
    sum1=abs(2*(slist[mid-1]-slist[mp])-(slist[-1]-slist[mp]))
  if mid==mp:
    sum3=sum2
  else:
    sum3=abs(2*(slist[mid+1]-slist[mp])-(slist[-1]-slist[mp]))

  if sum2<=sum1 and sum2<=sum3:
    return 0
  elif sum1<sum2:
    return -1
  elif sum2>sum3:
    return 1    
  
slist=[]
asum=0
for a in alist:
  asum+=a
  slist.append(asum)
#print(alist)
#print(slist)

dic_lp,dic_rp={},{}
for mp in range(1,N-2):  
  #lp
  l,r=0,mp
  while(l<=r):
    mid=(l+r)//2
    ret=bs_left(mid,mp)
    if ret==1:
      l=mid+1
    elif ret==-1:
      r=mid-1
    else:
      dic_lp[mp]=mid
      #print(mp,mid)
      break
  
  #rp
  l,r=mp+1,N-2
  while(l<=r):
    mid=(l+r)//2
    ret=bs_right(mid,mp)
    if ret==1:
      l=mid+1
    elif ret==-1:
      r=mid-1
    else:
      dic_rp[mp]=mid
      #print(mp,mid)
      break
    
min_diff=10**9
for mp in range(1,N-2):
  lp=dic_lp[mp]
  rp=dic_rp[mp]
  
  sum1=slist[lp]
  sum2=slist[mp]-slist[lp]
  sum3=slist[rp]-slist[mp]
  sum4=slist[-1]-slist[rp]
  #print(sum1,sum2,sum3,sum4)
  
  sum_list=[sum1,sum2,sum3,sum4]
  sum_list.sort()
  diff=sum_list[-1]-sum_list[0]
  #print(lp,mp,rp,diff)
  if diff<min_diff:
    min_diff=diff    
  
print(min_diff)

### FIXED CODE ###

N=int(input())
alist=list(map(int,input().split()))

slist=[]
asum=0
for a in alist:
  asum+=a
  slist.append(asum)
#print(alist)
#print(slist)

dic_lp,dic_rp={},{}
lp,rp=0,2
for mp in range(1,N-2):
  #p1
  mindiff=slist[mp]
  while(lp<mp+1):
    sum1=slist[lp]
    sum2=slist[mp]-slist[lp]
    
    diff=abs(sum1-sum2)
    if mindiff>diff:
      lp+=1
      mindiff=diff
    else:
      lp-=1
      dic_lp[mp]=lp
      #print(mp,lp)
      break
    
  rp=max(rp,mp+1)
  mindiff=slist[-1]-slist[mp]
  while(rp<N):
    sum1=slist[-1]-slist[rp]
    sum2=slist[rp]-slist[mp]
    
    diff=abs(sum1-sum2)
    if mindiff>diff:
      rp+=1
      mindiff=diff
    else:
      rp-=1
      dic_rp[mp]=rp
      #print(mp,rp)
      break
    
min_diff=10**9
for mp in range(1,N-2):
  lp=dic_lp[mp]
  rp=dic_rp[mp]
  
  sum1=slist[lp]
  sum2=slist[mp]-slist[lp]
  sum3=slist[rp]-slist[mp]
  sum4=slist[-1]-slist[rp]
  #print(sum1,sum2,sum3,sum4)
  
  sum_list=[sum1,sum2,sum3,sum4]
  sum_list.sort()
  diff=sum_list[-1]-sum_list[0]
  #print(lp,mp,rp,diff)
  if diff<min_diff:
    min_diff=diff
    #print(min_diff)
  
print(min_diff)
### BUGGY CODE ###

import sys
HK,WK,M = map(int, input().split())
hw_s = [list(map(int, input().split())) for _ in range(M)]
W = [0 for _ in range(WK+1)]
H = [0 for _ in range(HK+1)]
HW = [[0 for i in range(WK+1)] for _ in range(HK+1)]
# 3*10^5

for i, hw in enumerate(hw_s):
    h=hw[0]
    w=hw[1]

    H[h] += 1
    W[w] += 1

    HW[h][w] = 1

HMAXES = []
WMAXES = []
answer = 0
HMAX = max(H)
WMAX = max(W)


# 3*10^5
for i, h in enumerate(H):
    if h == HMAX:
        HMAXES.append(i)
for i, w in enumerate(W):
    if w == WMAX:
        WMAXES.append(i)
# 3*10^5
answer = HMAX + WMAX
for h in HMAXES:
    for w in WMAXES:
        if HW[h][w] == 0:
            print(answer)
            sys.exit(0)
print(answer-1)

### FIXED CODE ###

import sys
HK,WK,M = map(int, input().split())
W = [0] * (WK + 1)
H = [0] * (HK + 1)
HW = {}
# 3*10^5

for i in range(M):
    h, w = map(int, input().split())

    H[h] += 1
    W[w] += 1

    HW[(h,w)] = 1

answer = 0
HMAX = max(H)
WMAX = max(W)

HEMAX = H.count(HMAX)
WEMAX = W.count(WMAX)
if HEMAX * WEMAX > M:
    print(HMAX + WMAX)
    sys.exit(0)

HMAXES = [i for i, _ in enumerate(H) if _ == HMAX]
WMAXES = [i for i, _ in enumerate(W) if _ == WMAX]

# 3*10^5
answer = HMAX + WMAX
for h in HMAXES:
    for w in WMAXES:
        if not (h,w) in HW:
            print(answer)
            sys.exit(0)
print(answer-1)
### BUGGY CODE ###

# coding: utf-8
# Here your code !
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Oct  9 22:12:40 2017

@author: kitagenbakurou
"""
import numpy as np
import copy

def insert(pos, s, x):
  return x.join([s[:pos], s[pos:] ])

s = input()
N = len(s)

i = 0
cnt = 0
flag = True
while i < N - i - 1 : 
    if s[i] != s[N - i - 1] : 
        if s[i] == "x" : 
            cnt += 1
            s = insert(N - i  , s , "x")
        elif s[N - i - 1] == "x" : 
            cnt += 1
            s = insert(i , s , "x")
        else : 
            flag = False
            break
    N = len(s)
    i += 1

if flag : 
    print(cnt)
else : 
    print(-1)

### FIXED CODE ###

# coding: utf-8
# Here your code !
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Oct  9 22:12:40 2017

@author: kitagenbakurou
"""
import numpy as np
import copy

def insert(pos, s, x):
  return x.join([s[:pos], s[pos:] ])

s = input()
N = len(s)

i = 0
j = N - 1
cnt = 0
flag = True
while i < j : 
    if s[i] != s[j] : 
        if s[i] == "x" : 
            cnt += 1
            #s = insert(N - i  , s , "x")
            i += 1
            j += 0
        elif s[j] == "x" : 
            cnt += 1
            #s = insert(i , s , "x")
            i +=0
            j -= 1
        else : 
            flag = False
            break
    else : 
        i += 1
        j -= 1

if flag : 
    print(cnt)
else : 
    print(-1)
### BUGGY CODE ###

n, q = map(int, input().split())
graph = [[0]*(n) for _ in range(n)]

for _ in range(n-1):
    a, b = map(lambda x: int(x)-1, input().split())
    graph[a][b] = graph[b][a] = 1

values = [0]*n
for _ in range(q):
    p, x = map(int, input().split())
    values[p-1] += x

def dfs(v1, parent, add):
    values[v1] += add
    for v2 in range(n):
        if not graph[v1][v2]:
            continue
        if v2 == parent:
            continue
        dfs(v2, v1, values[v1])


dfs(0, 0, 0)

print(*values)

### FIXED CODE ###

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n, q = map(int, input().split())

graph = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(lambda x: int(x)-1, input().split())
    graph[a].append(b)
    graph[b].append(a)

values = [0]*n
for _ in range(q):
    p, x = map(int, input().split())
    values[p-1] += x

def dfs(v1, parent, add):
    values[v1] += add
    for v2 in graph[v1]:
        if v2 == parent:
            continue
        dfs(v2, v1, values[v1])

dfs(0, 0, 0)
print(*values)