require 'set'
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)}

dp = (1..h).map{(1..w).map{nil}+[Set[0]]}+[(1..w).map{Set[0]}]
max = a.zip(b).map{|ta,tb|ta.zip(tb).map{|ua,ub|(ua-ub).abs}.max}.max

(0...h).each{|th|
  (0...w).each{|tw|
    d = a[th][tw]-b[th][tw]
    s1 = dp[th-1][tw]
    s2 = dp[th][tw-1]
    dst = Set[*s1.map{|i|(i+d).abs} +
                s1.map{|i|(i-d).abs} +
                s2.map{|i|(i+d).abs} +
                s2.map{|i|(i-d).abs} ]
    dst.select!{|i|i<=max}
    dp[th][tw] = dst
  }
}
p dp[h-1][w-1].min