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]}]
mid1 = (h+w)/2
mid2 = h+w-mid1
(0...h).each{|th|
  (0...w-th).each{|tw|
    d = a[th][tw]-b[th][tw]
    s1 = dp[th-1][tw]
    s2 = dp[th][tw-1]
    dp[th][tw] = 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} ]
  }
}
(h-1).downto(0){|th|
  (w-1).downto([w-1-th+1,0].max){|tw|
    d = a[th][tw]-b[th][tw]
    s1 = dp[th+1][tw]
    s2 = dp[th][tw+1]
    dp[th][tw] = 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} ]
  }
}

(0...h).each{|th|
  dp[th][w] = Set[10e9]
}
dp[h] = (0...w).map{Set[10e9]}

p (0...[h,w].min).map{|th|
  a = dp[th][w-th-1]
  b = dp[th+1][w-th-1] + dp[th][w-th]
  a.to_a.product(b.to_a).map{|ta,tb|(ta-tb).abs}.min
}.min
