CodeEff/ECCO huggingface dataset:
# Original solution
input = int(eval(input()))
buf = input
result = 1
for i in range(2,input+1):
    if buf%i==0:
        result *= (1 - 1/i)
    while buf%i==0:
        buf /= i
result *= input
print((int(result)))

# Optimized solution
def phi(n):
    r = n
    i = 2
    while i*i <= n:
        if n%i==0:
            r -= r/i
            while n%i==0:
                n /= i
        i += 1
    if n>1: r-= r/n
    return r

n = int(eval(input()))
print((int(phi(n))))
# Original solution
while 1:
	n,m = list(map(int,input().split()))
	if n == 0: break
	EBY = [input().split() for i in range(n)]
	EBY = [[e,int(b),int(y)] for e,b,y in EBY]
	for i in range(m):
		year = int(input())
		for j in range(n):
			if EBY[j][2]-EBY[j][1] < year <= EBY[j][2]:
				print(EBY[j][0], year-EBY[j][2]+EBY[j][1])
				break
		else:
			print("Unknown")

# Optimized solution
while 1:
	n,m = list(map(int,input().split()))
	if n == 0: break
	EBY = [input().split() for i in range(n)]
	EBY = [[e,int(y)-int(b),int(y)] for e,b,y in EBY]
	for i in range(m):
		year = int(input())
		for j in range(n):
			if EBY[j][1] < year <= EBY[j][2]:
				print(EBY[j][0], year-EBY[j][1])
				break
		else:
			print("Unknown")
# Original solution
from math import floor,ceil
def f(b,n):
    if n < b:
        return n
    else:
        return f(b,floor(n/b))+n%b
n = int(eval(input()))
s = int(eval(input()))

for b in range(2,320000):
    if f(b,n)==s:
        print(b)
        exit()

if n==s:
    print((s+1))
    exit()

d = {}
for i in range(1,ceil(n**0.5)):
    m = f(n//i,n)
    if n//i==n//(i+1):
        d[i] = (m,m)
    else:
        M = f(n//(i+1)+1,n)
        d[i] = (m,M)

for i in range(ceil(n**0.5)-1,0,-1):
    m,M = d[i]
    if m<=s<=M and (s-m)%i==0:
        print((n//i-(s-m)//i))
        exit()

print((-1))

# Optimized solution
def f(b,n):
    if n < b:
        return n
    else:
        return f(b,n//b)+n%b

def get_divisor(n):
    res = []
    for i in range(1,int(n**0.5)+1):
        if n%i==0:
            res.append(i)
            res.append(n//i)
    res = list(set(res))
    res.sort()
    return res

n = int(eval(input()))
s = int(eval(input()))
ans = -1

for b in range(2,int(n**0.5)+1):
    if f(b,n)==s:
        ans = b
        break

if ans==-1 and n>=s:
    div = get_divisor(n-s)
    div.append(n)
    for b in div:
        b += 1
        if f(b,n)==s:
            ans = b
            break

print(ans)
# Original solution
N = int(eval(input()))
S = int(eval(input()))

def f(b,n):
    if n//b==0:
        return n
    else:
        return n%b + f(b,n//b)

ans = -1
if S==N:
    ans = N+1
else:
    for b in range(2,int(N**.5)+1):
        if f(b,N)==S:
            ans = b
            break

if ans == -1:
    for i in range(int(N**.5)+1,0,-1):
        b = (N-S)//i + 1
        if b>1:
            if f(b,N)==S:
                ans = b
                break

print(ans)

# Optimized solution
# python3 (3.4.3)
import sys
input = sys.stdin.readline

# main
N = int(eval(input()))
S = int(eval(input()))

def f(b,n):
    if n < b:
        return n
    else:
        return f(b, n//b) + n%b

ans = -1
if S==N:
    ans = N+1
else:
    # bをN^(1/2)まで全探索
    for b in range(2, int(N**.5)+1):
        if f(b,N)==S:
            ans = b
            break

# まだ答えが見つからなければ
if ans == -1:
    # bがN^(1/2)より大きい可能性が残っており、b進数で表すと2桁
    # 1 <= p < b, N = pb+q <= pb < p^2, p > N^(1/2) なので pで全探索
    # p+q = S より b = (N-S)/p+1
    for p in range(int(N**.5)+1,0,-1):
        b = (N-S)//p + 1
        # bが1のときは対象外にしないと無限ループ！
        if b >= 2 and f(b,N)==S:
            ans = b
            break

print(ans)
# Original solution
import math
import sys

n = int(eval(input()))
s = int(eval(input()))

def f(b, n):
    if n == 0:
        return 0
    return f(b, n//b) + n % b

if s > n:
    print((-1))
    sys.exit(0)

sn = math.ceil(math.sqrt(n))
for i in range(2, sn+1):
    if f(i, n) == s:
        print(i)
        sys.exit(0)

ans = n + 1
for i in range(1, sn+1):
    if (n - s) % i == 0:
        b = (n-s)//i + 1
        if b >= 2 and f(b, b * i + n%b) == s:
            ans = min((ans, b))
if s == n:
    print((n+1))
elif ans < n + 1:
    print(ans)
else:
    print((-1))


# Optimized solution
import sys
n = int(eval(input()))
s = int(eval(input()))
def f(b, n):
    if n < b:
        return n
    else:
        return f(b, n//b) + n % b

if s == n:
    print((n+1))
    sys.exit(0)
if n < s:
    print((-1))
    sys.exit(0)
     
ans = None
for i in range(2, n):
    if i * i > n:
        break
    if f(i, n) == s:
        ans = i
        break

for i in range(1, n):
    if i * i > n:
        break
    if (n - s) % i == 0:
        b = (n - s) // i + 1
        if f(b, n) == s:
            if ans is None:
                ans = (n - s) // i + 1
            else:
                ans = min((ans, (n - s) // i + 1))

if ans is None:
    print((-1))
else:
    print(ans)
# Original solution
n = int(eval(input()))
s = int(eval(input()))
if s == 1 and n == 1:
    print((2))
    exit()
if s > n:
    print((-1))
    exit()
if n%2 == 0 and n > s > n//2:
    print((-1))
    exit()
if n%2 == 1 and n > s > n//2+1:
    print((-1))
    exit()

ans = float("INF")
for i in range(2,int(n**0.5)+1):
    x = n
    count = 0
    while x >= i:
        count += x%i
        x //= i
    if count + x == s:
        print(i)
        exit()
now = 2
li = int(n**0.5)+1
while True:

    x = n//now
    if x < li:
        break
    count = 0
    y = n
    while y >= x:
        count += y%x
        y //= x
    count += y
    if count <= s and (s-count)%now == 0:
        z = x-n//(now+1)
        if (s-count)//now < z:
            ans = x-(s-count)//now
    now += 1

if s == n:
    print((min(ans,n+1)))
else:
    print((min(ans,n-s+1)))


# Optimized solution
n = int(eval(input()))
s = int(eval(input()))
if s == n:
    print((n+1))
    exit()
if s > n%2+n//2:
    print((-1))
    exit()
    

ans = float("INF")
for i in range(2,int(n**0.5)+1):
    x = n
    count = 0
    while x >= i:
        count += x%i
        x //= i
    if count + x == s:
        print(i)
        exit()
now = 2
li = int(n**0.5)+1
while True:

    x = n//now
    if x < li:
        break
    count = 0
    y = n
    while y >= x:
        count += y%x
        y //= x
    count += y
    if count <= s and (s-count)%now == 0:
        z = x-n//(now+1)
        if (s-count)//now < z:
            ans = x-(s-count)//now
    now += 1



print((min(ans,n-s+1)))
# Original solution
import math
import os
import sys
from functools import lru_cache

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N, S = [int(sys.stdin.readline()) for _ in range(2)]


@lru_cache(maxsize=None)
def f(b, n):
    if n < b:
        return n
    return f(b, int(n // b)) + n % b


def bisect_left_callable(fn, x, lo, hi):
    """
    lo から hi-1 のうち、fn の結果が x 以上となる、最も左の値
    bisect.bisect_left と同じ
    https://docs.python.org/ja/3/library/bisect.html
    :param callable fn:
    :param x:
    :param int lo: 最小値
    :param int hi: 最大値 + 1
    :return: lo <= ret <= hi
    """
    while lo < hi:
        mid = (lo + hi) // 2
        if fn(mid) < x:
            lo = mid + 1
        else:
            hi = mid
    return lo


if S > N:
    print((-1))
    exit()
if S == N:
    print((N + 1))
    exit()

# #########
# b = 2
# ans = -1
# prev = 0
# while b <= N:
#     if f(b, N) > prev:
#         print(b, N, f(b, N))
#     prev = f(b, N)
#     b += 1
# ##########

b = 2
ans = -1
while b <= math.sqrt(N) + 1:
    if f(b, N) == S:
        ans = b
        break
    b += 1
if ans >= 0:
    print(ans)
    exit()

for i in range(1, int(math.sqrt(N)) + 1):
    hi = N // i + 1
    lo = N // (i + 1) + 1
    b = bisect_left_callable(lambda x: -f(x, N), -S, lo, hi)
    if f(b, N) == S:
        ans = b
print(ans)



# Optimized solution
import math
import os
import sys
from functools import lru_cache

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N, S = [int(sys.stdin.readline()) for _ in range(2)]


@lru_cache(maxsize=None)
def f(b, n):
    if n < b:
        return n
    return f(b, int(n // b)) + n % b


if S > N:
    print((-1))
    exit()
if S == N:
    print((N + 1))
    exit()

# #########
# b = 2
# ans = -1
# prev = 0
# while b <= N:
#     if f(b, N) > prev:
#         print(b, N, f(b, N))
#     prev = f(b, N)
#     b += 1
# ##########

b = 2
ans = -1
while b <= math.sqrt(N) + 1:
    if f(b, N) == S:
        ans = b
        break
    b += 1
if ans >= 0:
    print(ans)
    exit()

ans = INF
# b > sqrt(N) のとき 2 桁になるので上位桁を全探索する
for d2 in range(1, int(math.sqrt(N)) + 1):
    if (N - S) % d2 == 0:
        b = int(1 + (N - S) // d2)
        if b >= 2 and f(b, N) == S:
            ans = min(ans, b)
if ans == INF:
    print((-1))
else:
    print(ans)
# Original solution
import math
n = int(eval(input()))
s = int(eval(input()))

def c(b):
    cur = n
    acc = 0
    while cur != 0:
        div, mod = divmod(cur, b)
        acc += mod
        cur = div
    return acc

def f():
    if n == s:
        return (n + 1)
    if n < s:
        return -1
    rt = math.ceil(math.sqrt(n))
    for b in range(2, rt + 1):
        if c(b) == s: return b
    for p in range(rt, 0, -1):
        q = s - p
        x = n - q
        b = x // p
        # if x % p == 0 and b > q and b > p and q >= 0:
        if c(b) == s:
            return b
    return -1
    
print((f()))        

# Optimized solution
import math
n = int(eval(input()))
s = int(eval(input()))

def c(b):
    cur = n
    acc = 0
    while cur != 0:
        div, mod = divmod(cur, b)
        acc += mod
        cur = div
    return acc

def g(b):
    if b > n: return n
    return sum(divmod(n,b))

def f():
    if n == s:
        return (n + 1)
    if n < s:
        return -1
    rt = math.ceil(math.sqrt(n))
    for b in range(2, rt + 1):
        if c(b) == s: return b
    for p in range(rt - 1, 0, -1):
        q = s - p
        x = n - q
        b = x // p
        if g(b) == s:
            return b
    return -1
    
print((f()))
# Original solution
from math import floor, sqrt
INF = 10 ** 20


def f(b, n):
    if n < b:
        return n
    else:
        return f(b, n // b) + (n % b)


def main():
    n = int(eval(input()))
    s = int(eval(input()))

    ans = INF
    # 1桁
    if n == s:
        ans = min(ans, n + 1)

    # 2桁
    for p in range(1, int(sqrt(n)) + 1):
        q = s - p
        b = (n - q) // p
        if 2 <= b and f(b, n) == s:
            ans = min(ans, b)

    # 3桁以上
    for b in range(2, int(sqrt(n)) + 1):
        if f(b, n) == s:
            ans = min(ans, b)

    print((-1 if ans == INF else ans))

if __name__ == '__main__':
    main()


# Optimized solution
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt, floor
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def f(b, n):
    if n < b:
        return n
    return f(b, n // b) + n % b


def main():
    N = int(eval(input()))
    S = int(eval(input()))

    if N == S:
        print((N + 1))
        return
    if N < S:
        print((-1))
        return

    ans = INF
    for b in range(2, int(sqrt(N)) + 1):
        if f(b, N) == S:
            ans = min(ans, b)

    bs = set()
    t = N - S
    for i in range(1, int(sqrt(N)) + 1):
        if t % i == 0:
            bs.add(i + 1)
            bs.add((t // i) + 1)
    
    for b in bs:
        if b == 1:
            continue
        if f(b, N) == S:
            ans = min(ans, b)

    if ans == INF:
        print((-1))
    else:
        print(ans)


if __name__ == '__main__':
    main()
# Original solution
n=int(eval(input()))
s=int(eval(input()))
if s>n:
  print((-1))
elif s==n:
  print((n+1))
else:
  # 2<=b<=n**0.5 で全探索
  for b in range(2,int(n**0.5)+1):
    tmp=0
    n0=n
    while n0>0:
      tmp+=n0%b
      n0//=b
    if tmp==s:
      print(b)
      exit()
  # n**0.5<bについて
  for p in range(int(n**0.5)+1,0,-1):
    b=(n-s)//p+1
    tmp=0
    n0=n
    while n0>0:
      tmp+=n0%b
      n0//=b
    if tmp==s:
      print(b)
      exit()
  print((-1))



# Optimized solution
n=int(eval(input()))
s=int(eval(input()))
if s>n:
  print((-1))
elif s==n:
  print((n+1))
else:
  # 2<=b<=n**0.5 で全探索
  for b in range(2,int(n**0.5)+1):
    tmp=0
    n0=n
    while n0>0:
      tmp+=n0%b
      n0//=b
    if tmp==s:
      print(b)
      exit()
  # n**0.5<bについて
  for p in range(int(n**0.5)+1,0,-1):
    b=(n-s)/p+1
    if not b.is_integer():continue
    b=int(b)
    tmp=0
    n0=n
    while n0>0:
      tmp+=n0%b
      n0//=b
    if tmp==s:
      print(b)
      exit()
  print((-1))
# Original solution
import math

n = int(eval(input()))
s = int(eval(input()))

r = 0

if n == s:
    print((n + 1))
    exit()
elif n < s:
    print((-1))
    exit()

nn = int(math.sqrt(n)) + 1

for i in range(2, nn):
    nt = n
    st = 0

    while nt > 0:
        st += nt % i
        nt //= i

    if st == s:
        print(i)
        exit()
    
for i in range(nn, 0, -1):
    nt = n
    st = 0

    b = (n - s) // i + 1
    
    while nt > 0:
        st += nt % b
        nt //= b

    if st == s:
        print(b)
        exit()

print((-1))

# Optimized solution
import math

n = int(eval(input()))
s = int(eval(input()))


r = 0

if n == s:
    print((n + 1))
    exit()
elif n < s:
    print((-1))
    exit()

sq = int(math.sqrt(n))


# 2 <= d <= √n
for i in range(2, sq+1):
    nt = n
    st = 0

    while nt > 0:
        st += nt % i
        nt //= i

    if st == s:
        print(i)
        exit()
    
# √n < d <= n
for i in range(sq+1, 0, -1):
    b = (n - s) // i + 1
    
    st = n % b + n // b

    if n // b != b and st == s:
        print(b)
        exit()

print((-1))
# Original solution
def primeFactor(N):
    i, n, ret, d, sq = 2, N, {}, 2, 99
    while i <= sq:
        k = 0
        while n % i == 0: n, k, ret[i] = n//i, k+1, k+1
        if k > 0 or i == 97: sq = int(n**(1/2)+0.5)
        if i < 4: i = i * 2 - 1
        else: i, d = i+d, d^6
    if n > 1: ret[n] = 1
    return ret

def divisors(N):
    pf = primeFactor(N)
    ret = [1]
    for p in pf:
        ret_prev = ret
        ret = []
        for i in range(pf[p]+1):
            for r in ret_prev:
                ret.append(r * (p ** i))
    return sorted(ret)

def f(b, n):
    re = 0
    while n:
        re += n % b
        n //= b
    return re

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

for b in divisors(abs(N-S)):
    b += 1
    if f(b, N) == S:
        print(b)
        break
else:
    print((-1))


# Optimized solution
def primeFactor(N):
    i, n, ret, d, sq = 2, N, {}, 2, 99
    while i <= sq:
        k = 0
        while n % i == 0: n, k, ret[i] = n//i, k+1, k+1
        if k > 0 or i == 97: sq = int(n**(1/2)+0.5)
        if i < 4: i = i * 2 - 1
        else: i, d = i+d, d^6
    if n > 1: ret[n] = 1
    return ret

def divisors(N):
    pf = primeFactor(N)
    ret = [1]
    for p in pf:
        ret_prev = ret
        ret = []
        for i in range(pf[p]+1):
            for r in ret_prev:
                ret.append(r * (p ** i))
    return sorted(ret)

def f(b, n):
    re = 0
    while n:
        re += n % b
        n //= b
    return re

N = int(eval(input()))
S = int(eval(input()))
if N < S:
    print((-1))
elif N == S:
    print((N+1))
else:
    for b in divisors(N-S):
        b += 1
        if f(b, N) == S:
            print(b)
            break
    else:
        print((-1))
# Original solution
n = int(eval(input()))
s = int(eval(input()))
rtn = -1* int( -1 * (n**0.5))
def f(b,n):
    if n<b:
        return n
    return f(b,n//b) + n % b
if n < s or (n // 2)+1 < s < n :
    print("-1")
elif s == n:
    print((n+1))
elif s == n//2 +1:
    if n % 2 == 1:
        print((n//2 + 1))
    else:
        print("-1")
elif (n//3) + 2 < s <= n//2:
    print((n-s+1))
else:
    x = n // s
    if s - (n //(x+1))  <= x:
        x = x + 1
    if 5 < n/(x -1) - n/x :
        for b in range(n//x, n//(x-1)):
            if f(b,n) == s:
                print(b)
                exit()
        for i in range(x-1,2,-1):
            for b in range(n//i + (n//x)//(n//i), n//i + (n//x)//(n//i)  + (2*n//x - n//(x+1))//(x-i)):
                if f(b,n) == s:
                    print(b)
                    exit()
    else:
        for b in range(2,rtn+1):
            if f(b,n) == s:
                print(b)
                exit()
        for p in range(rtn,0,-1):
            if (n-s) % p == 0:
                b = (n-s)//p + 1
                if f(b,n) == s:
                    print(b)
                    exit()
    print((n-s+1))


# Optimized solution
n = int(eval(input()))
s = int(eval(input()))
rtn = -1* int( -1 * (n**0.5))
def f(b,n):
    if n<b:
        return n
    return f(b,n//b) + n % b
if n < s or (n // 2)+1 < s < n :
    print("-1")
elif s == n:
    print((n+1))
elif s == n//2 +1:
    if n % 2 == 1:
        print((n//2 + 1))
    else:
        print("-1")
elif (n//3) + 2 < s <= n//2:
    print((n-s+1))
else:
    for b in range(2,rtn+1):
        if f(b,n) == s:
            print(b)
            exit()
    for p in range(rtn,0,-1):
        if (n-s) % p == 0:
            b = (n-s)//p + 1
            if f(b,n) == s:
                print(b)
                exit()
    print((n-s+1))
# Original solution
from math import floor, sqrt, ceil


def f(b, n):
    if b < 2:
        return 0

    if n < b:
        return n

    return f(b, floor(n / b)) + n % b


n = int(eval(input()))
s = int(eval(input()))

if n == s:
    print((n + 1))
    exit()

for b in range(2, floor(sqrt(n)) + 1):
    if f(b, n) == s:
        print(b)
        exit()

for p in range(1, ceil(sqrt(n)))[::-1]:
    b = (n - s) / p + 1
    if b.is_integer():
        b = int(b)
        if 1 <= p < b and 0 <= s - p < b:
            print(b)
            exit()

print((-1))


# Optimized solution
from math import floor, sqrt, ceil


def f(b, n):
    if n < b:
        return n

    return f(b, floor(n / b)) + n % b


n = int(eval(input()))
s = int(eval(input()))

if n == s:
    print((n + 1))
    exit()

for b in range(2, floor(sqrt(n)) + 1):
    if f(b, n) == s:
        print(b)
        exit()

for p in range(1, ceil(sqrt(n)))[::-1]:
    b = (n - s) / p + 1
    if b.is_integer():
        b = int(b)
        if 1 <= p < b and 0 <= s - p < b:
            print(b)
            exit()

print((-1))
# Original solution
import math
n = int(eval(input()))
s = int(eval(input()))

result = -1
if n == s:
  result = n + 1
elif s > n:
  pass
else:
  sqrt_n = math.floor(math.sqrt(n))
  for b in range(2, sqrt_n + 1):
    temp_n = n
    temp_b = 0
    while temp_n >= b:
      temp_b += temp_n % b
      temp_n = temp_n // b
    if temp_b + temp_n == s:
      result = b
      break
  if result == -1:
    for quot in range(sqrt_n, 0, -1):
      if n - s % quot != 0:
        b = (n - s) // quot + 1
        temp_n = n
        temp_b = 0
        while temp_n >= b:
          temp_b += temp_n % b
          temp_n = temp_n // b
        if temp_b + temp_n == s:
          result = b
          break
print(result)

# Optimized solution
import math
n = int(eval(input()))
s = int(eval(input()))

result = -1
if n == s:
  result = n + 1
elif s > n:
  pass
else:
  temp_sqrt_n = math.floor(math.sqrt(n))
  for b in range(2, temp_sqrt_n + 1):
    temp_n = n
    temp_b = 0
    while temp_n >= b:
      temp_b += temp_n % b
      temp_n = temp_n // b
    if temp_b + temp_n == s:
      result = b
      break
  if result == -1:
    temp_sqrt_n = math.floor(math.sqrt(n - 1))
    for quot in range(temp_sqrt_n, 0, -1):
      if n - s % quot != 0:
        b = (n - s) // quot + 1
        if quot + n % b == s:
          result = b
          break
print(result)
# Original solution
import sys
input = sys.stdin.readline
H, W = list(map(int, input().split()))
A = []
for _ in range(H):
    A.append(list(map(int, input().split())))
B = []
C = []
for y in range(H):
    B.append(list(map(int, input().split())))
    C.append(list(abs(a-b) for (a, b) in zip(A[y], B[y])))
VMAX = 80*159+1
dp = [[[0] * VMAX for _ in range(W)] for _ in range(H)]
dp[0][0][C[0][0]] = 1
for y in range(H):
    for x in range(W):
        if y==0 and x==0:
            continue
        for v in range(VMAX):
            if y-1 >= 0 and dp[y-1][x][v]:
                if abs(v+C[y][x]) < VMAX:
                    dp[y][x][abs(v+C[y][x])] = 1
                dp[y][x][abs(v-C[y][x])] = 1
            if x-1 >= 0 and dp[y][x-1][v]:
                if abs(v+C[y][x]) < VMAX:
                    dp[y][x][abs(v+C[y][x])] = 1
                dp[y][x][abs(v-C[y][x])] = 1
ans = -1
for v in range(VMAX):
    if dp[-1][-1][v]:
        ans = v
        break
print(ans)

# Optimized solution
import sys
input = sys.stdin.readline
H, W = list(map(int, input().split()))
A = []
for _ in range(H):
    A.append(list(map(int, input().split())))
B = []
C = []
for y in range(H):
    B.append(list(map(int, input().split())))
    C.append(list(abs(a-b) for (a, b) in zip(A[y], B[y])))
VMAX = 80*159+1
dp = [[0 for _ in range(W)] for _ in range(H)]
dp[0][0] |= 1 << C[0][0]
for y in range(H):
    for x in range(W):
        if y==0 and x==0:
            continue
        if y-1 >= 0:
            dp[y][x] |= dp[y-1][x] << C[y][x]
            dp[y][x] |= dp[y-1][x] >> C[y][x]
            d = C[y][x]+1
            r = format(dp[y-1][x] & (2**d-1), '0'+str(d)+'b')
            r = r[::-1]
            dp[y][x] |= int(r, 2)
        if x-1 >= 0:
            dp[y][x] |= dp[y][x-1] << C[y][x]
            dp[y][x] |= dp[y][x-1] >> C[y][x]
            d = C[y][x]+1
            r = format(dp[y][x-1] & (2**d-1), '0'+str(d)+'b')
            r = r[::-1]
            dp[y][x] |= int(r, 2)
ans = -1
for v in range(VMAX):
    if dp[-1][-1] >> v & 1:
        ans = v
        break
print(ans)
# Original solution
h,w=list(map(int,input().split()))
a=[list(map(int,input().split())) for i in range(h)]
b=[list(map(int,input().split())) for i in range(h)]

c=[[0]*w for i in range(h)]
for i in range(h):
  for j in range(w):
    c[i][j]=abs(a[i][j]-b[i][j])

dp=[[[0]*(80*(h+w)+1) for i in range(w)] for j in range(h)]
dp[0][0][c[0][0]]=1
for i in range(h):
  for j in range(w):
    for k in range(80*(i+j)+1):
      if dp[i-1][j][abs(k-c[i][j])]==1 or dp[i][j-1][abs(k-c[i][j])]==1 or dp[i-1][j][k+c[i][j]]==1 or dp[i][j-1][k+c[i][j]]:
        dp[i][j][k]=1

for i in range(80*(h+w)+1):
  if dp[h-1][w-1][i]==1:
    print(i)
    break

# Optimized solution
h,w=list(map(int,input().split()))
a=[list(map(int,input().split())) for i in range(h)]
b=[list(map(int,input().split())) for i in range(h)]

c=[[0]*w for i in range(h)]
for i in range(h):
  for j in range(w):
    c[i][j]=abs(a[i][j]-b[i][j])

dp=[[[] for i in range(w)] for j in range(h)]
dp[0][0].append(c[0][0])
for i in range(h):
  for j in range(w):
    for k in range(len(dp[i-1][j])):
      dp[i][j].append(abs(dp[i-1][j][k]-c[i][j]))
      dp[i][j].append(dp[i-1][j][k]+c[i][j])
    for k in range(len(dp[i][j-1])):
      dp[i][j].append(abs(dp[i][j-1][k]-c[i][j]))
      dp[i][j].append(dp[i][j-1][k]+c[i][j])
    dp[i][j]=list(set(dp[i][j]))
print((min(dp[h-1][w-1])))