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 + 100
dp = Array.new(H + 1){Array.new(W + 1){Array.new(mx + 1, false)}}
dp[0][0][ds[0][0]] = true
(0 ... H).each do |i|
  (0 ... W).each do |j|
    (0 .. mx).each do |k|
      if dp[i][j][k] == true
        dp[i + 1][j][k + ds[i + 1][j]] = true if k + ds[i + 1][j] <= mx
        dp[i + 1][j][(k - ds[i + 1][j]).abs] = true
        dp[i][j + 1][k + ds[i][j + 1]] = true if k + ds[i][j + 1] <= mx
        dp[i][j + 1][(k - ds[i][j + 1]).abs] = true
      end
    end
  end
end
puts dp[H - 1][W - 1].index(&:itself)