h,w = gets.split.map(&:to_i)
a = (1..h).map{gets.split.map(&:to_i)}
b = (1..h).map{gets.split.map(&:to_i)}
ab = a.zip(b).map{|ta,tb|ta.zip(tb).map{|ua,ub|(ua-ub).abs}}

max = ab.map(&:max).max
zero = 1 << (h+w)*max/2

dp = (1..h).map{(1..w).map{nil}+[0]}+[(1..w).map{0}]

dp[0][-1] = dp[-1][0] = zero

(0...h).each{|th|
  abth = ab[th]
  dpth = dp[th]
  dpth1 = dp[th-1]
  (0...w).each{|tw|
    d = abth[tw]
    s = dpth1[tw] | dpth[tw-1]
    dst = (s << d) | (s >> d) 
    dpth[tw] = dst
  }
}
ans = dp[h-1][w-1]
p (0..max).find{|i|
  ans & (zero << i) > 0 ||
  ans & (zero >> i) > 0
}