Harryxun/stf_run huggingface dataset:
x,y,a,b,c=map(int,input().split())
p=sorted(list(map(int,input().split())))[a-x::]
q=sorted(list(map(int,input().split())))[b-y::]
r=sorted(list(map(int,input().split())))[::-1]
if c<x+y:
  r+=[0]*(x+y-c)
i=0
j=0
ans=0
for k in range(x+y):
  if r[k]>p[i] and i<x:
    ans+=r[k]
    i+=1
  elif r[k]>q[j] and j<y:
    ans+=r[k]
    j+=1
  else:
    break
print(ans+sum(p[i:])+sum(q[j:]))

### FIXED CODE ###

x,y,a,b,c=map(int,input().split())
p=sorted(list(map(int,input().split())))[a-x::]
q=sorted(list(map(int,input().split())))[b-y::]
r=sorted(list(map(int,input().split())))[::-1]
p.append(10**9)
q.append(10**9)
if c<x+y:
  r+=[0]*(x+y-c)
i=0
j=0
ans=0
for k in range(x+y):
  if r[k]>p[i] and i<x and p[i]<=q[j]:
    ans+=r[k]
    i+=1
  elif r[k]>q[j] and j<y:
    ans+=r[k]
    j+=1
  else:
    break
if i==x:
  p1=0
else:
  p1=sum(p[i:x])
if j==y:
  q1=0
else:
  q1=sum(q[j:y])
print(ans+p1+q1)
a, b, c = map(int,input().split())
if c - b = b - a:
	print("Yes")
else:
	print("No")


### FIXED CODE ###

a, b, c = map(int,input().split())
if c - b == b - a:
	print("YES")
else:
	print("NO")
sys.setrecursionlimit(10**７)

def main():
    n, q = map(int, input().split())
    # バラバラに持ちたいなら
    # for i in range(n-1):
    #     a, b = map(int, input().split())
    # for i in range(q):
    #     q, x = map(int, input().split())
    ab = [list(map(int, input().split())) for i in range(n-1)]
    px = [list(map(int, input().split())) for i in range(q)]
    # print(ab)
    # print(qx)
    # indexがノード
    adj = [[] for _ in range(n+1)]
    for a, b in ab:
            adj[a].append(b)
            adj[b].append(a)
    # print(adj)
    # indexが直接初めにノードに加えられる得点
    v = [0] * (n+1)
    for p,x in px:
        v[p] += x

    def dfs(cur, parent):
        for chl in adj[cur]:
            if chl == parent: 
                continue
            v[chl] += v[cur] 
            dfs(chl, cur)

    dfs(1, 0)
    ans = ' '.join(map(str,v[1:]))
    print(ans)

if __name__ == '__main__':
    main()

### FIXED CODE ###

import sys
from collections import deque
# sys.setrecursionlimit(10**7)

def main():
    n, q = map(int, input().split())
    ab = [list(map(int, input().split())) for i in range(n-1)]
    px = [list(map(int, input().split())) for i in range(q)]
    adj = [[] for _ in range(n+1)]
    for a, b in ab:
            adj[a].append(b)
            adj[b].append(a)
    # indexが直接初めにノードに加えられる得点
    v = [0] * (n+1)
    for p,x in px:
        v[p] += x
    # 子をindexで、親ノードを要素で
    parent = [None] * (n+1)
    q = deque([1])
    while q:
        par = q.pop()
        for chl in adj[par]:
            if chl == parent[par]:
                continue
            v[chl] += v[par]
            parent[chl] = par
            q.append(chl)
    ans = ' '.join(map(str,v[1:]))
    print(ans)
    
    # def dfs(cur, parent):
    #     for chl in adj[cur]:
    #         print('p', parent)
    #         if chl == parent: 
    #             continue
    #         v[chl] += v[cur]
    #         print('chl,cur', chl, cur)
    #         dfs(chl, cur)

    # dfs(1, 1)
    # ans = ' '.join(map(str,v[1:]))
    # print(ans)

if __name__ == '__main__':
    main()
X,Y=list(map(int,input().split()))

import sys
if (2*Y-X)%3!=0 or (2*X-Y)%3!=0:
  print(0)
  sys.exit()
  
x=(2*Y-X)//3
y=(2*X-Y)//3
#(x+y)Cxを求める
fac=[0 for i in range(x+y+1)]
inv=[0 for i in range(x+y+1)]
finv=[0 for i in range(x+y+1)]
#初期条件
p=1000000007
fac[0]=fac[1]=1
inv[1]=1
finv[0]=finv[1]=1
#テーブルの作成
for i in range(2,x+y+1):
  fac[i]=fac[i-1]*i%p
  #p=(p//a)*a+(p%a) a^(-1)=-(p//a)*(p%a)^(-1)
  inv[i]=(-(p//i)*inv[p%i])%p
  finv[i]=finv[i-1]*inv[i]%p
#求める
print((fac[x+y]*finv[x]%p)*finv[y]%p)




### FIXED CODE ###

X,Y=list(map(int,input().split()))

import sys
if (2*Y-X)%3!=0 or (2*X-Y)%3!=0:
  print(0)
  sys.exit()
if (2*Y-X)<0 or (2*X-Y)<0:
  print(0)
  sys.exit()
x=(2*Y-X)//3
y=(2*X-Y)//3
#(x+y)Cxを求める
fac=[0 for i in range(x+y+1)]
inv=[0 for i in range(x+y+1)]
finv=[0 for i in range(x+y+1)]
#初期条件
p=1000000007
fac[0]=fac[1]=1
inv[1]=1
finv[0]=finv[1]=1
#テーブルの作成
for i in range(2,x+y+1):
  fac[i]=fac[i-1]*i%p
  #p=(p//a)*a+(p%a) a^(-1)=-(p//a)*(p%a)^(-1)
  inv[i]=(-(p//i)*inv[p%i])%p
  finv[i]=finv[i-1]*inv[i]%p
#求める

print((fac[x+y]*finv[x]%p)*finv[y]%p)
n=input()
if n[0]=='7' or n[1] == '7' or n[2] == '7':
	print("yes")
else:
  	print("no)

### FIXED CODE ###

N = input()
if N[0] == '7' or N[1] == '7' or N[2] == '7':
   print("Yes")
else:
   print("No")
r = input()
print(2 * r * r)


### FIXED CODE ###

r = int(input())
print(3 * r * r)
n=input()
l=len(n)
a,b=0,0
while True:
    b+=n[a]
    a+=1
    if a==l:
      break
print(b)

### FIXED CODE ###

a=input()
b=0
for i in range(len(a)):
    b+=int(a[i])
print(max((len(a)-1)*9+int(a[0])-1,b))
n,m = (int(i) for i in input().split()) 
a = [int(i) for i in input().split()] 
e = [[int(i) for i in input().split()] for i in range(m)]

a.sort()
for i, l in enumerate(e):
  c = 0
  for j in range(l[0]):
    if (a[j] < l[1]):
      c = c+1
    else:
      break
  for j in range(c):
    a.pop(0)
  f = 0
  for j, al in enumerate(a):
    if (j+1) == len(a):
      break
    if(a[j] < l[1] and a[j+1] > l[1]):
      for k in range(c):
        a.insert(j+1, l[1])
      f = 1
      break
  if (f == 0):
    if (a[0] > l[1]):
      for j in range(c):
        a.insert(0,l[1])
    else:
      for j in range(c):
        a.insert(len(a),l[1])
  #print(a)
print(sum(a))

### FIXED CODE ###

n,m = (int(i) for i in input().split()) 
a = [int(i) for i in input().split()] 
e = [[int(i) for i in input().split()] for i in range(m)]

a.sort()
e.sort(key=lambda x: x[1], reverse=True)
c = 0
arr = []
for i, el in enumerate(e):
  for j in range(el[0]):
    #print(c,n)
    if (c < n):
        arr.append(el[1])
        c += 1
    else:
        break
  if (c >= n):
    break
retval = 0
#print(arr)
for i in range(c):
  if (a[i] < arr[i]):
    a[i] = arr[i]
    #print(a[i],arr[i])
  else:
    break
  #print(a)
print(sum(a))
#ABC072-C
import collections
import itertools

n = int(input())
a = list(map(int,input().split()))
 
cnt = [0] * 100000
a.sort()

for i in range(a):
    cnt[i] += 1
    cnt[i+1] += 1
    cnt[i+2] += 2
    
    

    
        
print(max(cnt))


### FIXED CODE ###

#ABC072-C
n = int(input())
a = list(map(int,input().split()))
 
cnt = [0] * 100002


for i in a:
    cnt[i] += 1
    cnt[i+1] += 1
    cnt[i+2] += 1
    
    

    
        
print(max(cnt))
n,k = map(int,input().split())
print(input().replace(s[k-1],s[k-1].lower()))

### FIXED CODE ###

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

print(S[0:K-1] + S[K-1].lower() + S[K:N])
import collections
n = int(input())
a = list(map(int,input().split()))
c = collections.Counter(a)

if len(c) > 3:
    print("No")
    exit()
elif len(c) == 3:
    if n % 3 != 0:
        print("No")
        exit()
    p = []
    for k , v in c.items():
        if v != n//3:
            print("No")
            exit()
        p.append(k)
    if (p[0] ^ p[1]) ^ p[2] != 0:
        print("No")
        exit()
elif len(c) == 2:
    if n % 3 != 0:
        print("No")
        exit()
    for k,v in c.items():
        if k == 0:
            if v != n//3:
                print("No")
                exit()
        elif:
            if v != 2*n//3:
                print("No")
                exit()
elif len(c) == 1:
    for k in c.keys():
        if k != 0:
            print("No")
            exit()
            
print("Yes")

### FIXED CODE ###

import collections
n = int(input())
a = list(map(int,input().split()))
c = collections.Counter(a)

if len(c) > 3:
    print("No")
    exit()
elif len(c) == 3:
    if n % 3 != 0:
        print("No")
        exit()
    p = []
    for k , v in c.items():
        if v != n//3:
            print("No")
            exit()
        p.append(k)
    if (p[0] ^ p[1]) ^ p[2] != 0:
        print("No")
        exit()
elif len(c) == 2:
    if n % 3 != 0:
        print("No")
        exit()
    for k,v in c.items():
        if k == 0:
            if v != n//3:
                print("No")
                exit()
        else:
            if v != 2*n//3:
                print("No")
                exit()
elif len(c) == 1:
    for k in c.keys():
        if k != 0:
            print("No")
            exit()
            
print("Yes")
from functools import reduce
import numpy as np

mod=10**9+7

def mat(list):
    return reduce(lambda a,b: a*b%mod, list)


N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
A = np.array(sorted(A, key=abs, reverse=True))
A_pn = A < 0
# print(A_pn)
if N == np.count_nonzero(A_pn):
    if K % 2 == 0:
        print(mat(A[:K]))
    else:
        print(mat(A[-K:]))
elif np.count_nonzero(A_pn[:K]) % 2 == 0:
    print(mat(A[:K]))
else:
    for a in A[K:]:
        if a >= 0:
            last = a
    print(mat(A[:K-1])*last)


### FIXED CODE ###

from functools import reduce
import numpy as np

mod=10**9+7

def mat(list):
    return reduce(lambda a,b: a*b%mod, list)


N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
A = np.array(sorted(A, key=abs, reverse=True))
A_pn = A < 0
if N == np.count_nonzero(A_pn):
    if K % 2 == 0:
        print(mat(A[:K]))
    else:
        print(mat(A[-K:]))
elif np.count_nonzero(A_pn[:K]) % 2 == 0:
    print(mat(A[:K]))
elif N==K:
    print(mat(A))
else:
    negative_all = np.where(A_pn)[0]
    negative_rest = np.count_nonzero(negative_all>=K)
    if negative_rest == 0:
        # 負の数が残っていない
        A[negative_all[-1]] = A[K]
        print(mat(A[:K]))
    elif negative_rest == N-K:
        # 正の数が残っていない
        last_positive = np.where(A_pn[:K]==False)[0][-1]
        A[last_positive] = A[K]
        print(mat(A[:K]))
    elif len(negative_all)-negative_rest==K:
        # 負の数しか選択されていない
        last_negative = negative_all[negative_all<K][-1]
        next_positive = np.where(A_pn==False)[0][0]
        A[last_negative] = A[next_positive]
        print(mat(A[:K]))
    else:
        last_negative = negative_all[negative_all<K][-1]
        next_negative = negative_all[negative_all>=K][0]
        positive_all = np.where(A_pn==False)[0]
        last_positive = positive_all[positive_all<K][-1]
        next_positive = positive_all[positive_all>=K][0]
        if A[last_negative]*A[next_negative] > A[last_positive]*A[next_positive]:
            A[last_positive] = A[next_negative]
        else:
            A[last_negative] = A[next_positive]
        print(mat(A[:K]))
S=input()
ACGT=["A","C","G","T"]
m=0
for i in range(len(S)):
  j=0
  while S[i+j] in ACGT:
    j+=1   
  m=max(m,j)
print(m)

### FIXED CODE ###

S=input()
SS=S+"X"
ACGT=["A","C","G","T"]
m=0
for i in range(len(S)):
  j=0
  while SS[i+j] in ACGT:
    j+=1   
  m=max(m,j)
print(m)
import collections

n = int(input())
v = list(map(int, input().split()))

even_odd_length = n / 2
replace_total = 0

list_odd = v[0::2]
list_even = v[1::2]

dict_c_odd = dict(collections.Counter(list_odd))
dict_c_even = dict(collections.Counter(list_even))

if len(set(v)) == 1:
    print(int(even_odd_length))
else:
    max_count_c_odd = max(dict_c_odd.values())
    max_count_c_even = max(dict_c_even.values())
    max_c_odd_keys = [k for k, v in dict_c_odd.items() if v == max_count_c_odd]
    max_c_even_keys = [k for k, v in dict_c_even.items() if v == max_count_c_even]
    
    if max_count_c_odd > max_count_c_even:
        if max_c_odd_keys[0] in dict_c_even:
            del dict_c_even[max_c_odd_keys[0]]
        max_count_c_even = max(dict_c_even.values())
    elif max_count_c_odd < max_count_c_even:
        if max_c_even_keys[0] in dict_c_odd:
            del dict_c_odd[max_c_even_keys[0]]
        max_count_c_odd = max(dict_c_odd.values())
    else:
        if len(max_c_odd_keys) > len(max_c_even_keys):
            if max_c_even_keys[0] in dict_c_odd:
                del dict_c_odd[max_c_even_keys[0]]
            max_count_c_odd = max(dict_c_odd.values())
        elif: len(max_c_odd_keys) > len(max_c_even_keys):
            if max_c_odd_keys[0] in dict_c_even:
                del dict_c_even[max_c_odd_keys[0]]
            max_count_c_even = max(dict_c_even.values())
        else:
            if max_c_odd_keys[0] in dict_c_even:
                del dict_c_even[max_c_odd_keys[0]]
            max_count_c_even = max(dict_c_even.values())
    replace_total += (n - max_count_c_odd - max_count_c_even)
    print(int(replace_total))

### FIXED CODE ###

import collections

n = int(input())
v = list(map(int, input().split()))
even_odd_length = n / 2
replace_total = 0

list_odd = v[0::2]
list_even = v[1::2]

dict_c_odd = dict(collections.Counter(list_odd))
dict_c_even = dict(collections.Counter(list_even))
dict_c_even_bk = dict_c_even.copy()
dict_c_odd_bk = dict_c_odd.copy()

replace_total_tmp = 0
replace_total_tmp2 = 0
max_count_c_odd = max(dict_c_odd.values())
max_count_c_even = max(dict_c_even.values())
max_count_c_even_bk = max_count_c_even
max_c_odd_keys = [k for k, v in dict_c_odd.items() if v == max_count_c_odd]
max_c_even_keys = [k for k, v in dict_c_even.items() if v == max_count_c_even]

if len(set(v)) == 1:
    print(int(even_odd_length))
else:    
    if max_c_odd_keys[0] in dict_c_even:
        del dict_c_even[max_c_odd_keys[0]]
    max_count_c_even = max(dict_c_even.values())
    replace_total_tmp = (n - max_count_c_odd - max_count_c_even)
    
    dict_c_even = dict_c_even_bk
    max_count_c_even = max_count_c_even_bk
    
    if max_c_even_keys[0] in dict_c_odd:
        del dict_c_odd[max_c_even_keys[0]]
    max_count_c_odd = max(dict_c_odd.values())
    replace_total_tmp2 = (n - max_count_c_odd - max_count_c_even)
    
    if replace_total_tmp >= replace_total_tmp2:
         replace_total = replace_total_tmp2
    else:
         replace_total = replace_total_tmp
    print(replace_total)
a,b,t = int(input().split())
s = int(t//a)
print(b*s)

### FIXED CODE ###

a,b,t = map(int, input().split())
s = int(t//a)
print(b*s)
r_l = [1,2,3,4,5,6,7,8,9,10,11,12,21,22,23,32,33,34,43,44,45,54,55,56,65,66,67,76,77,78,87,88,89,98,99,100,101,110,111,112,113,121,122,123,210,211,212,221,222,223,232,233,234]
N = int(input())
print(r_l[N-1])


### FIXED CODE ###

n = input()
n_lis = n.split(" ")

print(n_lis[2],n_lis[0],n_lis[1])
a,b=map(int,input().split())

def lcm(x, y):
    return (x * y) // math.gcd(x, y)

print(lcm(a, b))

### FIXED CODE ###

import fractions

def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)

a,b=map(int,input().split())

print(lcm(a, b))
N = int(input())
A = sorted(list(map(int,input().split())))
B = list(map(int,input().split()))
C = sorted(list(map(int,input().split())))

ans = 0
#print(A)
#print(C)
for b in B:
    lenA = bisect.bisect_left(A,b) #競合時は無事無視される
    lenC = N - bisect.bisect_left(C,b) if b not in C else N - C.index(b) - 1
    ans += lenA*lenC
    #print(lenA,lenC)
    
print(ans)

### FIXED CODE ###

import bisect

N = int(input())
A = sorted(list(map(int,input().split())))
B = list(map(int,input().split()))
C = sorted(list(map(int,input().split())))

ans = 0
#print(A)
#print(C)
for b in B:
    lenA = bisect.bisect_left(A,b)
    lenC = N - bisect.bisect(C,b)
    ans += lenA*lenC
    #print(lenA,lenC)
    
print(ans)
h,w=map(int,input().split())
a=[[j for j in input()] for i in range(h)]
for i in range(h):
  if a[i].count('.')==w:
    a[i]=[]
while [] in a:
  a.remove([])
for i in range(w):
  c=0
  for j in range(len(a)):
    if a[j][i]=='.':
      c+=1
  if c==len(a):
    for j in range(len(a)):
      a[j][i]='!'
for i in range(len(a)):
  while '!' in a[i]:a
    a[i].remove('!')
for i in a:
  print(*i,sep='')

### FIXED CODE ###

h,w=map(int,input().split())
a=[[j for j in input()] for i in range(h)]
for i in range(h):
  if a[i].count('.')==w:
    a[i]=[]
while [] in a:
  a.remove([])
for i in range(w):
  c=0
  for j in range(len(a)):
    if a[j][i]=='.':
      c+=1
  if c==len(a):
    for j in range(len(a)):
      a[j][i]='!'
for i in range(len(a)):
  while '!' in a[i]:
    a[i].remove('!')
for i in a:
  print(*i,sep='')
import collections
I = [int(_) for _ in open(0).read().split()]
N = I[0]
AB = I[1:]
G = collections.defaultdict(set)
for a, b in zip(AB[::2], AB[1::2]):
    G[a].add(b)
    G[b].add(a)
color = [None] * (N + 1)


def dfs(G, v, cur):
    color[v] = cur
    for w in G[v]:
        if color[w] is None:
            dfs(G, w, 1 - cur)


dfs(G, 1, 0)
c=collections.Counter(color[1:])
cand1 = set(range(1, N + 1, 3))
cand2 = set(range(2, N + 1, 3))
cand3 = set(range(3, N + 1, 3))

i0, i1 = 0, 1
if c[0] > c[1]:
    i0, i1 = 1, 0

ans = [None] * (N + 1)
if c[i0] <= len(cand3):
    for i in range(1, N + 1):
        if color[i]==i0:
            ans[i] = cand3.pop()
        else:
            if cand1:
                ans[i] = cand1.pop()
            elif cand2:
                ans[i] = cand2.pop()
            elif cand3:
                ans[i] = cand3.pop()
else:
    for i in range(1, N + 1):
        if color[i] == i0:
            if cand2:
                ans[i] = cand2.pop()
            elif cand3:
                ans[i] = cand3.pop()
        else:
            if cand1:
                ans[i] = cand1.pop()
            elif cand3:
                ans[i] = cand3.pop()
print(*ans[1:])


### FIXED CODE ###

import collections
import sys
sys.setrecursionlimit(2*10**5)
I = [int(_) for _ in open(0).read().split()]
N = I[0]
AB = I[1:]
G = collections.defaultdict(set)
for a, b in zip(AB[::2], AB[1::2]):
    G[a].add(b)
    G[b].add(a)
color = [None] * (N + 1)


def dfs(G, v, cur):
    color[v] = cur
    for w in G[v]:
        if color[w] is None:
            dfs(G, w, 1 - cur)


dfs(G, 1, 0)
c=collections.Counter(color[1:])
cand1 = set(range(1, N + 1, 3))
cand2 = set(range(2, N + 1, 3))
cand3 = set(range(3, N + 1, 3))

i0, i1 = 0, 1
if c[0] > c[1]:
    i0, i1 = 1, 0

ans = [None] * (N + 1)
if c[i0] <= len(cand3):
    for i in range(1, N + 1):
        if color[i]==i0:
            ans[i] = cand3.pop()
        else:
            if cand1:
                ans[i] = cand1.pop()
            elif cand2:
                ans[i] = cand2.pop()
            elif cand3:
                ans[i] = cand3.pop()
else:
    for i in range(1, N + 1):
        if color[i] == i0:
            if cand2:
                ans[i] = cand2.pop()
            elif cand3:
                ans[i] = cand3.pop()
        else:
            if cand1:
                ans[i] = cand1.pop()
            elif cand3:
                ans[i] = cand3.pop()
print(*ans[1:])
import copy
N = int(input())
X = list(map(int,input().split()))
length = len(X)
X.sort()
bef = int(length/2)
aft = bef+1
for i in range(N):
    if(i<bef):
        print(X[bef])
    else:
        print(X[aft])

### FIXED CODE ###

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

sortx = sorted(X)
bef = sortx[N//2-1]
aft = sortx[N//2]
for i in X:
        print(aft if i <= bef else bef)
# -*- coding: utf-8 -*-
import sys
from collections import defaultdict
from collections import deque

H,W=map(int, sys.stdin.readline().split())
CH,CW=map(int, sys.stdin.readline().split())
CH-=1
CW-=1
DH,DW=map(int, sys.stdin.readline().split())
DH-=1
DW-=1
S=[ sys.stdin.readline().strip() for _ in range(W) ]

D=[(0,1),(0,-1),(1,0),(-1,0)]
WARP=[(1,1),(-1,1),(1,-1),(-1,-1),(2,0),(-2,0),(0,2),(0,-2),(1,2),(-1,2),(1,-2),(-1,-2),(2,1),(-2,1),(2,-1),(-2,-1),(2,2),(-2,2),(2,-2),(-2,-2)]

d=defaultdict(lambda: float("inf"))    #(h,w):スタート地点からその点までの距離

q=deque()
q.append((CH,CW))
d[(CH,CW)]=0

while q:
    fro_h, fro_w=q.popleft()
    #歩いて移動
    for dh,dw in D:
        to_h, to_w=fro_h+dh, fro_w+dw
        if 0<=to_h<H and 0<=to_w<W:
            if S[to_h][to_w]==".":
                alt=d[(fro_h,fro_w)]+0
                if alt<d[(to_h,to_w)]:
                    d[(to_h, to_w)]=alt
                    q.appendleft((to_h, to_w))
    #ワープ
    for dh,dw in WARP:
        to_h, to_w=fro_h+dh, fro_w+dw
        if 0<=to_h<H and 0<=to_w<W:
            if S[to_h][to_w]==".":
                alt=d[(fro_h,fro_w)]+1
                if alt<d[(to_h,to_w)]:
                    d[(to_h, to_w)]=alt
                    q.append((to_h, to_w))

if d[(DH,DW)]==float("inf"):
    print -1
else:
    print d[(DH,DW)]


### FIXED CODE ###

# -*- coding: utf-8 -*-
import sys
from collections import defaultdict, deque

H,W=map(int, sys.stdin.readline().split())
CH,CW=map(int, sys.stdin.readline().split())
CH-=1
CW-=1
DH,DW=map(int, sys.stdin.readline().split())
DH-=1
DW-=1
S=[ sys.stdin.readline().strip() for _ in range(H) ]

D=[(0,1),(0,-1),(1,0),(-1,0)]
WARP=[]
for dh in range(-2,3):
    for dw in range(-2,3):
        if abs(dh)+abs(dw)<=1: continue
        WARP.append((dh,dw))

d=defaultdict(lambda: float("inf")) #スタート地点から各マスまでの距離

q=deque()
q.append((0,CH,CW))
d[(CH,CW)]=0

while q:
    kyori, fro_h, fro_w=q.popleft()
    if d[(fro_h, fro_w)]<kyori: continue
    #歩いて移動
    for dh,dw in D:
        to_h, to_w=fro_h+dh, fro_w+dw
        if 0<=to_h<H and 0<=to_w<W and S[to_h][to_w]==".":
            if kyori < d[(to_h, to_w)]:
                d[(to_h, to_w)]=kyori
                q.appendleft((kyori, to_h, to_w))
    #ワープ
    for dh,dw in WARP:
        to_h, to_w=fro_h+dh, fro_w+dw
        if 0<=to_h<H and 0<=to_w<W and S[to_h][to_w]==".":
            if kyori+1 < d[(to_h, to_w)]:
                d[(to_h, to_w)]=kyori+1
                q.append((kyori+1, to_h, to_w))

if d[(DH,DW)]==float("inf"):
    print -1
else:
    print d[(DH,DW)]
import sys
readline = sys.stdin.readline

N, M, K = map(int, readline().split())
A = list(map(int, readline().split()))
B = list(map(int, readline().split()))

n = 0
m = 0
t = 0
for i in range(N):
  if t + A[i] <= K:
    n += 1
    t += A[i]
  else:
    break
for i in range(M):
  if t + B[i] <= K:
    m += 1
    t += B[i]
  else:
    break
max = n + m
for i in range(N):
  if n == 0 or m == M :
    break
  t -= A[n]
  n -= 1
  for j in range(M):
    if t + B[m + 1] <= K:
      m += 1
      t += B[m]
    else:
      break
    if m == M:
      break
  if n + m > max:
    max = n + m
print(max)

### FIXED CODE ###

import sys
readline = sys.stdin.readline

N, M, K = map(int, readline().split())
A = list(map(int, readline().split()))
B = list(map(int, readline().split()))

a = [0]
b = [0]
for i in range(N):
  a.append(a[i] + A[i])
for i in range(M):
  b.append(b[i] + B[i])

max = 0
j = M
for i in range(N+1):
  t = K - a[i]
  if t < 0:
    break
  while b[j] > t:
    j -= 1
  if i + j > max:
    max = i + j
print(max)
for char in input():
  S.append(char)
#route 1
C1=0
for i in range(len(S)):
  if i%2==0:
    if S[i]=='1':
      C1+=1
  else:
    if S[i]=='0':
      C1+=1

#route 2
S=X
C2=0
for n in range(len(S)):
  if n%2==0:
    if S[n]=='0':
      C2+=1
  else:
    if S[n]=='1':
      C2+=1

if C1>=C2:
  print(C1)
else:
  print(C2)

### FIXED CODE ###

S=[]
for char in input():
  S.append(char)
#route 1
C1=0
for i in range(len(S)):
  if i%2==0:
    if S[i]=='1':
      C1+=1
  else:
    if S[i]=='0':
      C1+=1

#route 2
C2=0
for n in range(len(S)):
  if n%2==0:
    if S[n]=='0':
      C2+=1
  else:
    if S[n]=='1':
      C2+=1

if C2>=C1:
  print(C1)
else:
  print(C2)
import collections

N = int(input())
x, y = [], []
for _ in range(N):
    _x, _y = map(int, input().split())
    x.append(_x)
    y.append(_y)

pq_cand = []
for i in range(N):
    for j in range(i + 1, N):
        pq_cand.append((x[j] - x[i], y[j] - y[i]))

pqc = collections.Counter(pq_cand)
_, x = max(pqc.items(), key=lambda x: x[1])
print(N - x)


### FIXED CODE ###

import collections

N = int(input())

if N == 1:
    print(1)
    exit()

x_y = []
for _ in range(N):
    x_y.append(list(map(int, input().split())))

x_y.sort(key=lambda x: (x[0], x[1]))
pq_cand = []
for i in range(N):
    for j in range(i + 1, N):
        pq_cand.append((x_y[j][0] - x_y[i][0], x_y[j][1] - x_y[i][1]))

pqc = collections.Counter(pq_cand)
_, x = max(pqc.items(), key=lambda x: x[1])
print(N - 1 - x + 1)
M=10**9+7;D=[];i=1;r=0;N,K=map(int,input().split())
while i*i<=N:
	if N%i==0:D.append(i);i*i<N and D.append(N//i)
	i+=1
D.sort();N=[]
for i in range(len(D)):
	N.append(pow(K,-~D[i]//2,M))
	for j in range(i):
		if D[i]%D[j]==0:N[i]=(N[i]-N[j])%M
	r=(r+(N[i]*D[i]*pow(2,M-2+D[i]%2,M)))%M
print(r)

### FIXED CODE ###

M=10**9+7;D=[];i=1;r=0;N,K=map(int,raw_input().split())
while i*i<=N:
	if N%i==0:D.append(i);i*i<N and D.append(N/i)
	i+=1
D.sort();N=[]
for i in range(len(D)):
	N.append(pow(K,-~D[i]/2,M))
	for j in range(i):
		if D[i]%D[j]==0:N[i]=(N[i]-N[j])%M
	r=(r+(N[i]*D[i]*pow(2,M-2+D[i]%2,M)))%M
print r