H, W = gets.split.map(&:to_i)
As = H.times.map{gets.split.map(&:to_i)}
Bs = H.times.map{gets.split.map(&:to_i)}

ds = (0 ... H).map{|i| (0 ... W).map{|j| (As[i][j] - Bs[i][j]).abs} + [0]} + [[0] * (W + 1)]
mx = ds.flatten.max * (H + W)
dp = Array.new(H){Array.new(W, 0)}
dp[0][0] |= 1 << (mx + ds[0][0])
dp[0][0] |= 1 << (mx - ds[0][0])
H.times do |i|
  W.times do |j|
    if i > 0
      dp[i][j] |= dp[i - 1][j] << ds[i][j]
      dp[i][j] |= dp[i - 1][j] >> ds[i][j]
    end
    if j > 0
      dp[i][j] |= dp[i][j - 1] << ds[i][j]
      dp[i][j] |= dp[i][j - 1] >> ds[i][j]
    end
  end
end
ans = dp.last.last
puts (0 .. mx).find{|x| ans[mx + x] == 1 || ans[mx - x] == 1}