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-1)){|di,dp|
  pre = 0
  dp.map!.with_index{|dpi,i|
    dt = di[i]
    s = dpi | pre
    pre = (s << dt) | (s >> dt)
  }
}[-1]

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