require 'set'
h,w = gets.split.map(&:to_i)
ab = (1..h).map{gets.split.map(&:to_i)}
h.times{|i|
  gets.split.map(&:to_i).each.with_index{|b,j|
    ab[i][j] = (ab[i][j] - b).abs
  }
}


dp = (1..h).map{(1..w).map{nil}+[Set[]]}+[(1..w).map{Set[]}]
max = (h+w)*ab.map(&:max).max/2

dp[0][-1] = dp[-1][0] = Set[0]

(0...h).each{|th|
  abth = ab[th]
  dpth = dp[th]
  dpth1 = dp[th-1]
  (0...w).each{|tw|
    d = abth[tw]
    s1 = dpth1[tw]
    s2 = dpth[tw-1]
    dst = Set[]
    (s1|s2).each{|i|
      if max >= t = i+d
        dst.add(t)
      end
      dst.add((i-d).abs)
    }
    dpth[tw] = dst
  }
}
p dp[h-1][w-1].min