H, W = gets.split.map(&:to_i)
MAP = Array.new(H) do
  gets.split.map(&:to_i)
end

H.times do |i|
  gets.split.map(&:to_i).each_with_index do |b, j|
    MAP[i][j] = (MAP[i][j] - b).abs
  end
end

DP = Array.new(H) do
  Array.new(W) do
    Hash.new(false)
  end
end

start_diff = MAP[0][0]
DP[0][0][start_diff] = DP[0][0][-start_diff] = true
# DP
H.times do |i|
  W.times do |j|
    diff = MAP[i][j]
    if (i > 0)
      DP[i-1][j].keys.each do |key|
        DP[i][j][key + diff] = DP[i][j][key - diff] = true
      end
    end
    if (j > 0)
      DP[i][j-1].keys.each do |key|
        DP[i][j][key + diff] = DP[i][j][key - diff] = true
      end
    end
  end
end
# puts MAP.to_s

# puts DP.to_s
# puts DP[H-1][W-1].to_s
puts DP[H-1][W-1].keys.map(&:abs).min
