h,w = gets.split.map(&:to_i)
a,b = 2.times.map{(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

ans = ab.each_with_object([zero]+[0]*w){|di,dp|
  pre = 0
  dp.replace(
    di.zip(dp).map{|dt,dpi|
      s = dpi | pre
      pre = (s << dt) | (s >> dt)
    }
  )
}[-1]

p (0..max).find{|i|ans & ((zero << i)|(zero >> i)) > 0}