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
  }
}
zero = 1 << 1000

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

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 (1..max).find{|i|
  ans & (zero << i) > 0 ||
  ans & (zero >> i) > 0
}