Harryxun/stf_run huggingface dataset:
import sys

def main(*args, **kwargs):
    # Initialize data
    H, W = map(int, input().split())
    total_cells = H * W
    existing_black_cells = 0

    grid = []
    for i in range(H):
        line = sys.stdin.readline()
        existing_black_cells += line.count('#')
        grid.append(line)

    # Initialize Search
    queue = deque()
    queue.append((0, 0))  # start point
    visited = set()
    movement = 0

    while len(queue) > 0:
        movement += 1
        for i in range(len(queue)):  # Process same level
            coodinate = queue.popleft()
            visited.add(coodinate)
            y = coodinate[0]
            x = coodinate[1]

            # Terminate condition
            if y == H - 1 and x == W - 1:
                print(total_cells - movement - existing_black_cells)
                return

            for dx, dy in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
                X = x + dx
                Y = y + dy
                adjacent = (Y, X)
                if 0 <= X < W and 0 <= Y < H and grid[Y][X] == '.' and adjacent not in visited:
                    queue.append(adjacent)
    print(-1)
    return
main()

### FIXED CODE ###

import sys
from collections import deque
    
def main(*args, **kwargs):
    # Initialize data
    H, W = map(int, sys.stdin.readline().split())
    total_cells = H * W
    existing_black_cells = 0
 
    grid = []
    for i in range(H):
        line = sys.stdin.readline()
        existing_black_cells += line.count('#')
        grid.append(line)
 
    # Initialize Search
    queue = deque()
    queue.append((0, 0))  # start point
    visited = set()
    visited.add((0, 0))
    movement = 0
 
    while len(queue) > 0:
        movement += 1
        for i in range(len(queue)):  # Process same level
            coodinate = queue.popleft()
            y = coodinate[0]
            x = coodinate[1]
 
            # Terminate condition
            if y == H - 1 and x == W - 1:
                print(total_cells - movement - existing_black_cells)
                return
 
            for dx, dy in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
                X = x + dx
                Y = y + dy
                adjacent = (Y, X)
                if 0 <= X < W and 0 <= Y < H and grid[Y][X] == '.' and adjacent not in visited:
                    queue.append(adjacent)
                    visited.add(adjacent)
    print(-1)
    return
main()
n = int(input())
if n==1:
  print(int(input())
else:
  ab = [[0] * n for i in range(3)]
  for i in range(n):
    ab[0][i], ab[1][i] = map(int, input().split())
    ab[2][i] = ab[0][i] + ab[1][i]
  sorted(ab, key=lambda x: x[2])
  print(sum(ab[0][0::2])-sum(ab[1][1::2]))


### FIXED CODE ###

# C
n = int(input())
if n==1:
    a, b =map(int, input().split())
    print(a)
else:
    def return_a(n):
        return n[0]
    def return_b(n):
        return n[1]
    ab = [[0] * 3 for i in range(n)]
    for i in range(n):
        ab[i][0], ab[i][1] = map(int, input().split())
        ab[i][2] = ab[i][0] + ab[i][1]
    ab = sorted(ab, key=lambda x: x[2], reverse=True)
    print(sum(map(return_a, ab[0::2])) - sum(map(return_b, ab[1::2])))
h,w = map(int,input().split())
maze = []
for _ in range(h):
  s = input()
  if s != '.'*w:
    maze.append(s)
h = len(maze)
skip = []
for j in range(w):
  for i in raneg(h):
    if maze[i][j] == '#':
      break
  else:
    skip.append(j)

for i in range(h):
  for j in range(w):
    if j not in skip:
      print(maze[i][j],end= '')
  print()

### FIXED CODE ###

h,w = map(int,input().split())
maze = []
for _ in range(h):
  s = input()
  if s != '.'*w:
    maze.append(s)
h = len(maze)
skip = []
for j in range(w):
  for i in range(h):
    if maze[i][j] == '#':
      break
  else:
    skip.append(j)

for i in range(h):
  for j in range(w):
    if j not in skip:
      print(maze[i][j],end= '')
  print()
def frogOne(N, arr):
    dparr= [-1 for i in range(N)]
    dparr[0] = 0
    dparr[1] = abs(arr[0]-arr[1])
    for num in range(2,N):
        dparr[num] = min(dparr[num-2]+ abs(arr[num]-arr[num-2]), dparr[num-1]+ abs(arr[num]-arr[num-1]))
    return dparr[-1]


 if __name__ == '__main__':
   N = int(input())
   arr = list(map(int,input().rstrip().split()))
   print(frogOne(N, arr))

### FIXED CODE ###

N = int(input())
arr = list(map(int,input().rstrip().split()))
dparr= [-1 for i in range(N)]
dparr[0] = 0
dparr[1] = abs(arr[0]-arr[1])
for num in range(2,N):
  dparr[num] = min(dparr[num-2]+ abs(arr[num]-arr[num-2]), dparr[num-1]+ abs(arr[num]-arr[num-1]))
print(dparr[-1])
n = int(input())
a = set(map(int, input().split()))
kinds = len(a)
if kinds % 2 == 1:
    print(kinds)
else:
    print(kinds - 1)

### FIXED CODE ###

x = int(input())
if x % 11 == 0:
    print(x // 11 * 2)
else:
    cnt = x // 11 * 2
    x -= x // 11 * 11
    while x > 0:
        if cnt % 2 == 0:
            x -= 6
        else:
            x -= 5
        cnt += 1
    print(cnt)
import sys
for s in sys.stdin:
 a,b=map(int,raw_input().split())
 print a-b

### FIXED CODE ###

import sys
for s in sys.stdin:
 a,b=map(int,s.split())
 print a-b
s=input()
if s="111":
  print(3)
elif s="110" or "101" or "011":
  print(2)
elif s="100" or "010" or "001":
  print(1)
else s="000":
  print(0)


### FIXED CODE ###

s=input()
print(s.count("1"))
from math import gcd
import sys

for line in sys.stdin:
    a, b = list(map(int, line.split()))
    print("{0} {1}".format(gcd(a, b), (a * b) / gcd(a, b)))

### FIXED CODE ###

import sys

def gcd(x, y):
    if x < y:
        x, y = y, x
    while y > 0:
        x, y = y, x % y
    return x

for line in sys.stdin:
    a, b = list(map(int, line.split()))
    print("{0} {1}".format(gcd(a, b), int((a * b) / gcd(a, b))))
import sys
sys.setrecursionlimit(10**6)
n,k = map(int,input().split())
h = list(map(int, input().split()))

dp = [0] * n
for i in range(k):
     dp[i]=abs(h[0]-h[i])
for i in range (k,n):
     for j in range(1,k):
          dp[i] = min(dp[i-k]+abs(h[i-k]-h[i]),dp[i-k-1]+abs(h[i-k-1]-h[i]))

print(dp[n-1])


### FIXED CODE ###

import numpy as np
N, K = (int(i) for i in input().split())  
h = np.array([int(i) for i in input().split()])
 
dp = np.empty(N, dtype='int')
 
for i in range(N):
  if i == 0:
    dp[i] = 0
  elif i - K < 0:
    dp[i] = np.min(np.abs(h[i] - h[:i]) + dp[:i])
  else:
    dp[i] = np.min(np.abs(h[i] - h[i-K:i]) + dp[i-K:i])
 
print(dp[N-1])
a = input()
b = input()

print(int(a) * int(b))
print(int(a) * 2 + int(b) * 2)

### FIXED CODE ###

[a, b] = [int(x) for x in input().split()]
print(a * b ,(a + b) * 2)
print('\n'.join([s[2] for s in sorted([input().split() + [i + 1] for i in range(int(input()))], key=lambda x:(x[0], -int(x[1])))]))

### FIXED CODE ###

print('\n'.join([l[2] for l in sorted(sorted([[l[1][0], int(l[1][1]), str(l[0] + 1)] for l in enumerate(input().split() for _ in range(int(input())))], key=lambda l: l[1], reverse=True), key=lambda l: l[0])]))
import math

n,s=map(int,input().split())
x=list(map(int,input().split()))
ans=0
#print(x)

for i in range(n-1):
    ans=math.gcd(abs(x[i]-s),abs(x[i+1]-s))

print(ans)

### FIXED CODE ###

import fractions
from functools import reduce
n,s=map(int,input().split())
x=list(map(int,input().split()))
#print(x)

for i in range(n):
    x[i]=abs(x[i]-s)

print(reduce(fractions.gcd,x))
while 1:
  h,w = map(int, raw_input().split())
  if h==w==0: break
  A = [0]*h
  for i in range(h):
    x = raw_input().replace(".", "1").replace("*", "0")
    A[i] = map(int, list(x))
  B = A[:]
  C = []
  for j in range(1,h):
    M = []
    for i in range(j,h):
      x = [e1 & e2 for e1, e2 in zip(B[i-j], A[i])]
      B[i-j] = x
      M.append(sorted("".join(map(str, x)).split("0"))[-1])
    C.append(len(sorted(M)[-1])*(j+1))
  print max(C)

### FIXED CODE ###

while 1:
  h,w = map(int, raw_input().split())
  if h==w==0: break
  M = [0]*(h+1)
  a=[0]*w
  for i in range(h):
    x = raw_input()
    b=[0]*w
    sp=-1
    C = [-1]*(h+1)
    for j in range(w):
      if x[j]=="*": c=0
      else: c=a[j]+1
      b[j]=c
      dw=c-sp
      if dw>0: C[sp+1:c+1]=[j]*dw
      elif dw<0:
        for k in range(c+1,sp+1): M[k]=max(M[k],j-C[k])
        C[c+1:sp+1]=[-1]*(-dw)
      sp=c
    for k in range(1,sp+1):
      if C[k]>=0: M[k]=max(M[k],w-C[k])
    a=b
  s=max([a*b for a,b in zip(M, range(h+1))])
  print s
S = input()
count = 0
res = []
for i in range(len(S)):
    if S[i] in ["A", "C", "G", "T"]:
        count += 1
    else:
        res.append(count)
        count = 0
print(max(res))


### FIXED CODE ###

s = input()
ans=0
now=0
for a in s:
    if a in ["A", "C", "G", "T"]:
        now+=1
    else:
        now=0
    ans=max(ans,now)
print(ans)
N = int(input())
a = list(map(int, input().split()))

a = sorted(a)
X = sum(a)

ans = []
for i in range(1, N - 1):
    ans.append(X - 2*sum(a[:i]))

print(min(ans))


### FIXED CODE ###

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

X = sum(a)
b = [a[0]]
for i in range(1, N - 1):
    b.append(b[i-1] + a[i])

ans = []
for i in range(0, N - 1):
    ans.append(abs(X - 2*b[i]))

print(min(ans))
n,a,b=map(int, input().split())
 
def comb(n, r,MOD):
  	r=min(r,n-r)
    p, q = 1, 1
    for i in range(r):
        p = p * (n-i) % MOD
        q = q * (i+1) % MOD
    return p * pow(q, MOD-2, MOD) % MOD
mod=10**9+7
 
if n==2:
    print(0)
else:
    print((pow(2,n,mod)-1-comb(n,a,mod)-comb(n,b,mod))%mod)

### FIXED CODE ###

n,a,b=map(int, input().split())
 
def comb(n, r,MOD):
    p, q = 1, 1
    for i in range(r):
        p = p * (n-i) % MOD
        q = q * (i+1) % MOD
    return p * pow(q, MOD-2, MOD) % MOD

mod=10**9+7
    
A=comb(n,a,mod)
B=comb(n,b,mod)  
C=pow(2,n,mod)

if n==2:
    print(0)
else:
    print((C-1-A-B)%mod)
,a,b=map(int,input().split())
if a<b:
    if b-(a+w) < 0:
        print(0)
    else:
        print(b-(a+w))
else:
    if a-(b+w) < 0:
        print(0)
    else:
        print(a-(b+w))

### FIXED CODE ###

w,a,b=map(int,input().split())
if a<b:
    if b-(a+w) < 0:
        print(0)
    else:
        print(b-(a+w))
else:
    if a-(b+w) < 0:
        print(0)
    else:
        print(a-(b+w))
import itertools
N,K=map(int,input().split())
L=range(1,N+1)
T=0
for i in itertools.permutations(L, r=3):
  int(a,b,c) for a,b,c in i:
    if a+b%K==0 and b+c%K==0 and c+a%K==0:
      T+=1
print(T) 

### FIXED CODE ###

N,K=map(int,input().split())
if K%2==0:
  a=N//(K//2)
  b=N//K
  c=a-b
  ans=b**3+c**3
  print(ans)
else:
  a=N//K
  ans=a**3
  print(ans)
import sys

n, m, *ab = map(int, sys.stdin.read().split())
ab = list(zip(*[iter(ab)] * 2))

root = list(range(n+1)); root[0] = None
height = [0] * (n + 1); height[0] = None
size = [1] * (n + 1); size[0] = None

def find_root(v):
    u = root[v]
    if u == v:
        return u
    w = find_root(u)
    root[v] = w
    return w

def unite(v, u):
    rv = find_root(v)
    ru = find_root(u)
    if rv == ru:
        return
    if height[v] >= height[u]:
        root[ru] = rv
        height[rv] = max(height[rv], height[ru] + 1)
        size[rv] += size[ru]
    else:
        root[rv] = ru
        size[ru] += size[rv]

def is_alone(v, rv, sv):
    return v == rv and sv == 1

def main():
    res = [0] * m
    for i in range(1, m):
        a, b = ab[m-i]
        ra = find_root(a)
        rb = find_root(b)
        sa = size[ra]
        sb = size[rb]
        
        bl_a = is_alone(a, ra, sa)
        bl_b = is_alone(b, rb, sb)

        if bl_a & bl_b:
            res[i] = res[i-1] + 1
        elif bl_a ^ bl_b:
            res[i] = res[i-1] + sa * sb
        else:
            if ra == rb:
                res[i] = res[i-1]
            else:
                res[i] = res[i-1] + sa * sb
        
        unite(a, b)

    all_pairs = n * (n - 1) // 2
    for i in res[::-1]:
        yield all_pairs - i
    
if __name__ == '__main__':
    ans = main()
    print(*ans, sep='\n')

### FIXED CODE ###

import sys

n, m, *ab = map(int, sys.stdin.read().split())
ab = list(zip(*[iter(ab)] * 2))

root = list(range(n+1)); root[0] = None
height = [0] * (n + 1); height[0] = None
size = [1] * (n + 1); size[0] = None

sys.setrecursionlimit(10 ** 9)
def find_root(v):
    u = root[v]
    if u == v:
        return u
    w = find_root(u)
    root[v] = w
    return w

def unite(v, u):
    rv = find_root(v)
    ru = find_root(u)
    if rv == ru:
        return
    if height[v] >= height[u]:
        root[ru] = rv
        height[rv] = max(height[rv], height[ru] + 1)
        size[rv] += size[ru]
    else:
        root[rv] = ru
        size[ru] += size[rv]

def is_alone(v, rv, sv):
    return v == rv and sv == 1

def main():
    res = [0] * m
    for i in range(1, m):
        a, b = ab[m-i]
        ra = find_root(a)
        rb = find_root(b)
        sa = size[ra]
        sb = size[rb]
        
        bl_a = is_alone(a, ra, sa)
        bl_b = is_alone(b, rb, sb)

        if bl_a & bl_b:
            res[i] = res[i-1] + 1
        elif bl_a ^ bl_b:
            res[i] = res[i-1] + sa * sb
        else:
            if ra == rb:
                res[i] = res[i-1]
            else:
                res[i] = res[i-1] + sa * sb
        
        unite(a, b)

    all_pairs = n * (n - 1) // 2
    for i in res[::-1]:
        yield all_pairs - i
    
if __name__ == '__main__':
    ans = main()
    print(*ans, sep='\n')
n, k, q = list(map(int, input().split()))
a = [int(input()) for i in range(q)]
p = [k - q] * n
for i in range(q):

    p[a - 1] += 1
for i in range(n):
    if(p[i] > 0):
        print('Yes')
    else:
        print('No')

### FIXED CODE ###

n, k, q = list(map(int, input().split()))
a = [int(input()) for i in range(q)]
p = [k - q] * n
for i in range(q):
    p[a[i] - 1] += 1
for i in range(n):
    if(p[i] > 0):
        print('Yes')
    else:
        print('No')
while 1:
    n,a,b=map(int,input().split())
    if n==a==b==0:break
    c=0;d=[1]*(n+1)
    for x in range(a):
        for i in range(x*b,n+1,a):d[i]=0
    print(sum(d))

### FIXED CODE ###

import re
while 1:
    n,m=map(int,input().split())
    if n==0:break
    p=[];s=0
    for _ in [0]*n:
        n,M=input().replace('*','[0-9]').split()
        p.append([re.compile(n),int(M)])
    for _ in [0]*m:
        l=input()
        for n,M in p:
            if n.search(l):
                s+=M;break
    print(s)
A, B = map(int, input().split())
q = (B＋A-3)//(A-1)
print(q)
  


### FIXED CODE ###

A, B = map(int, input().split())
q = -((-B+1)//(A-1))
 
print(q)
import sys
 
n, k = map(int, input().split())
c = list(map(int, input().split()))
 
maxNum = sys.maxsize
 
dp = [maxNum] * n
dp[0] = 0
 
for i in range(n):
    for j in range(1, k+1):
        if (0 <= i+j) & (i+j <= n-1):
            dp[i+j] = min(dp[i+j], dp[i] + abs(c[i] - c[i+j]))
 
print(dp[n-1])

### FIXED CODE ###

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

dp[0] = 0
dp[1] = abs(c[0] - c[1])

for i in range(2, n):
    dp[i] = min(dp[i - 1] + abs(c[i] - c[i - 1]), dp[i - 2] + abs(c[i] - c[i - 2]))

print(dp[n-1])
import math
#import numpy as np
import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)

def popcorn(i,k):
    num = 0
    ii = i
    while ii > 0:
        if ii&1:
            num += 1
        ii //= 2
    if i%num:
        return popcorn(i%num,k+1)
    else:
        return k+1

def main():
    n = int(ipt())
    x = list(map(int,input()))
    si = sum(x)
    su = si+1
    sd = si-1
    amu = 0
    amd = 0
    n2u = [1]
    if si == 2:
        n2d = [0]
    else:
        n2d = [1]
    for i in x[::-1]:
        if i:
            amu = (amu+n2u[-1])%su
            amd = (amd+n2d[-1])%sd
        n2u.append((n2u[-1]*2)%su)
        n2d.append((n2d[-1]*2)%sd)
    for i,xi in enumerate(x):
        if xi:
            ni = (amd-n2d[-2-i])%sd
            if si == 1:
                print(0)
            elif ni == 0:
                print(1)
            else:
                print(popcorn(ni,1))
        else:
            ni = (amu+n2u[-2-i])%su
            if ni == 0:
                print(1)
            else:
                print(popcorn(ni,1))


    return None

if __name__ == '__main__':
    main()


### FIXED CODE ###

import math
#import numpy as np
import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)

def popcorn(i,k):
    num = 0
    ii = i
    while ii > 0:
        if ii&1:
            num += 1
        ii //= 2
    if i%num:
        return popcorn(i%num,k+1)
    else:
        return k+1

def main():
    n = int(ipt())
    x = list(map(int,list(input())))
    si = sum(x)
    su = si+1
    sd = max(1,si-1)
    amu = 0
    amd = 0
    n2u = [1]
    if si == 2:
        n2d = [0]
    else:
        n2d = [1]
    for i in x[::-1]:
        if i:
            amu = (amu+n2u[-1])%su
            amd = (amd+n2d[-1])%sd
        n2u.append((n2u[-1]*2)%su)
        n2d.append((n2d[-1]*2)%sd)
    for i,xi in enumerate(x):
        if xi:
            ni = (amd-n2d[-2-i])%sd
            if si == 1:
                print(0)
            elif ni == 0:
                print(1)
            else:
                print(popcorn(ni,1))
        else:
            ni = (amu+n2u[-2-i])%su
            if ni == 0:
                print(1)
            else:
                print(popcorn(ni,1))


    return None

if __name__ == '__main__':
    main()
import sys
input = sys.stdin.readline
h,w=map(int,input().split())
a=[]
for i in range(h):
    a.append(list(map(int,input().split())))
b=[]
for i in range(h):
    b.append(list(map(int,input().split())))
dp=[[set([]) for i in range(w)] for j in range(h)]
dp[0][0].add(a[0][0]-b[0][0])
dp[0][0].add(b[0][0]-a[0][0])
for i in range(h):
    for j in range(w):
        if i==j==0:
            continue
        if i==0:
            for k in dp[i][j-1]:
                dp[i][j].add(k+a[i][j]-b[i][j])
                dp[i][j].add(k+b[i][j]-a[i][j])
        elif j==0:
            for k in dp[i-1][j]:
                dp[i][j].add(k+a[i][j]-b[i][j])
                dp[i][j].add(k+b[i][j]-a[i][j])
        else:
            for k in (dp[i][j-1]|dp[i-1][j]):
                dp[i][j].add(k+a[i][j]-b[i][j])
                dp[i][j].add(k+b[i][j]-a[i][j])             
#dp[h-1][w-1]
ans=10**10
for i in dp[h-1][w-1]:
    ans=min(ans,abs(i))
print(ans)


### FIXED CODE ###

a,b,c=map(int,input().split())
if a+b+c>=22:
    print('bust')
else:
    print('win')
S = input()
K = int(input())
if S[0] == "1":
    print(S[1])
else:
    print(S[0])


### FIXED CODE ###

S = input()
K = int(input())
if S[0] != "1":
    print(S[0])
else:
    ind = 0
    while S[ind] == "1" and ind < len(S) - 1:
        ind += 1
    if ind < K:
        print(S[ind])
    else:
        print("1")
while True:
	H, W = map(int, raw_input().split())
	if H == 0 and W == 0:
		break
	tile = []
	for i in range(H):
		tile.append(list(raw_input()))
	x = y = 0
	while True: 
		a = tile[y][x]
		tile[y][x] = 0
		if w == ".":
			print x, y;break
		elif w == ">":
			x += 1
		elif w == "v":
			y += 1
		elif w == "^":
			y -= 1
		elif w == "<": 
			x -= 1
		else:
			print "LOOP";break

### FIXED CODE ###

while True:
    h, w = map(int, raw_input().split())
    if w == 0 and h == 0:
        break
    tile = []
    for i in range(h):
        tile.append(list(raw_input()))
    x = y = 0
    while True:
        a = tile[y][x]
        tile[y][x] = 0
        if   a == ">": x += 1
        elif a == "<": x -= 1
        elif a == "^": y -= 1
        elif a == "v": y += 1
        elif a == ".": print x,y; break
        else: print "LOOP"; break
N,X=map(int,input().split())
x=list(map(int,input().split()))
absx=[]
for i in range(len(x)):
    absx.append(abs(x[i]-X))
import math
from functools import reduce

def gcd_list(numbers):
    return reduce(math.gcd, numbers)
print(gcd_list(absx))

### FIXED CODE ###

N,X=map(int,input().split())
x=list(map(int,input().split()))
absx=[]
for i in range(len(x)):
    absx.append(abs(x[i]-X))
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

from functools import reduce

def gcd_list(numbers):
    return reduce(gcd, numbers)
print(gcd_list(absx))
import numpy as np
i8 = np.int64
i4 = np.int32

def calc(dp1, dp2, k):
    dp2[k:] += dp1[:dp1.shape[0]-k]
    dp2[:dp2.shape[0]-k] += dp1[k:]

def solve(z, dp):
    zero = dp.shape[2] // 2
    dp[0, 0, zero + z[0, 0]] = 1
    dp[0, 0, zero - z[0, 0]] = 1
    for x in range(1, z.shape[1]):
        calc(dp[0, x - 1], dp[0, x], z[0, x])
    for y in range(1, z.shape[0]):
        calc(dp[y - 1, 0], dp[y, 0], z[y, 0])
        for x in range(1, z.shape[1]):
            calc(dp[y, x - 1], dp[y, x], z[y, x])
            calc(dp[y - 1, x], dp[y, x], z[y, x])
    for i in range(zero, dp.shape[2]):
        if dp[dp.shape[0] - 1, dp.shape[1] - 1, i] > 0:
            return i - zero
    return -1

def main2():
    stdin = open('/dev/stdin')
    H, W = map(int, stdin.readline().split())
    a = np.empty((H, W), i8)
    b = np.empty((H, W), i8)
    for i in range(H):
        for j, c in enumerate(map(int, stdin.readline().split())):
            a[i, j] = c
    for i in range(H):
        for j, c in enumerate(map(int, stdin.readline().split())):
            b[i, j] = c
    z = np.abs(a - b)
    if H > W:
        z = z.T.copy()
    dp = np.zeros((H, W, 80 * 80 * 80*2 + 1), i8)
    ans = solve(z, dp)
    print(ans)


main2()

### FIXED CODE ###

import numpy as np
i8 = np.int64
i4 = np.int32

def calc(dp1, dp2, k):
    dp2[k:] += dp1[:dp1.shape[0]-k]
    dp2[:dp2.shape[0]-k] += dp1[k:]

def solve(z, dp):
    zero = dp.shape[2] // 2
    dp[0, 0, zero + z[0, 0]] = 1
    dp[0, 0, zero - z[0, 0]] = 1
    for x in range(1, z.shape[1]):
        calc(dp[0, x - 1], dp[0, x], z[0, x])
    for y in range(1, z.shape[0]):
        calc(dp[y - 1, 0], dp[y, 0], z[y, 0])
        for x in range(1, z.shape[1]):
            calc(dp[y, x - 1], dp[y, x], z[y, x])
            calc(dp[y - 1, x], dp[y, x], z[y, x])
            if (x + y) % 10 == 0:
                dp[y, x] = dp[y, x] > 0
    for i in range(zero, dp.shape[2]):
        if dp[dp.shape[0] - 1, dp.shape[1] - 1, i] > 0:
            return i - zero
    return -1

def main2():
    stdin = open('/dev/stdin')
    H, W = map(int, stdin.readline().split())
    a = np.empty((H, W), i8)
    b = np.empty((H, W), i8)
    for i in range(H):
        for j, c in enumerate(map(int, stdin.readline().split())):
            a[i, j] = c
    for i in range(H):
        for j, c in enumerate(map(int, stdin.readline().split())):
            b[i, j] = c
    z = np.abs(a - b)
    dp = np.zeros((H, W, 80 * 80 *2 + 1), i8)
    ans = solve(z, dp)
    print(ans)


main2()
N = input()
TA = input()
T, A = TA.split()
Hinp = input()
H = Hinp.split()
ans = None
for x in range(H):
    _ans = T - x * 0.006
    if _ans == ans:
        print(ans)
        return
    if not ans or abs(A - _ans) < abs(A - ans):
        ans = _ans
print(ans)

### FIXED CODE ###

import sys
N = input()
TA = input()
T, A = TA.split()
Hinp = input()
H = Hinp.split()
ansnum = None
ans = 0
def getdiff(A, ans):
    return abs(max(ans, A) - min(ans, A))
def chk(A, _ans, ans):
    new_d, old_d = getdiff(A, _ans), getdiff(A, ans)
    if new_d < old_d:
        return True
    return False
for i, x in enumerate(H):
    _ans = int(T) - int(x) * 0.006
    if _ans == A:
        break
    if not ansnum or chk(int(A), _ans, ansnum):
        ans = i+1
        ansnum = _ans
print(ans)
a,b,c,d,e,f=map(int,input().split())
aa=0
ans=[0,0]
for i in range(0,f+1,a*100):
  for j in range(0,f+1-i,b*100):
    for k in range(0,e*(i+j)/100+1,c):
      for l in range(0,e*(i+j)/100+1-k,d):
        if i+j+k+l>f:
          continue
        if i+j>0 and aa<100*(k+l)/(i+j):
          aa=100*(k+l)/(i+j)
          ans=[i+j+k+l,k+l]
print(ans[0],ans[1])
        

### FIXED CODE ###

a,b,c,d,e,f=map(int,input().split())
aa=-1
ans=[0,0]
for i in range(0,f+1,a*100):
  for j in range(0,f+1-i,b*100):
    for k in range(0,int(e*(i+j)/100)+1,c):
      for l in range(0,int(e*(i+j)/100)+1-k,d):
        if i+j+k+l>f:
          continue
        if i+j>0 and aa<(k+l)/(i+j+k+l):
          aa=(k+l)/(i+j+k+l)
          ans=[i+j+k+l,k+l]
print(ans[0],ans[1])