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
    0
  end
end

BASE = 1300

diff = MAP[0][0]
DP[0][0] |= 1 << (BASE + diff)
DP[0][0] |= 1 << (BASE - diff)
# DP
H.times do |i|
  W.times do |j|
    diff = MAP[i][j]
    if (i > 0)
      tmp = DP[i-1][j]
      DP[i][j] |= tmp << diff
      DP[i][j] |= tmp >> diff
    end
    if (j > 0)
      tmp = DP[i][j-1]
      DP[i][j] |= tmp << diff
      DP[i][j] |= tmp >> diff
    end
  end
end

BASE.times do |i|
  if DP[H-1][W-1][BASE + i] == 1 || DP[H-1][W-1][BASE - i] == 1
    puts i.abs
    break
  end
end
