module IntSet = Set.Make (struct
  type t = int
  let compare = compare
end)

let () = Scanf.scanf "%d %d\n" @@ fun h w ->
  let ass =
    Array.init h @@ fun _ ->
      Array.init w @@ fun _ ->
        Scanf.scanf "%d " @@ fun a -> a in
  let bss =
    Array.init h @@ fun _ ->
      Array.init w @@ fun _ ->
        Scanf.scanf "%d " @@ fun b -> b in
  let dp = Array.make_matrix (h + 1) (w + 1) IntSet.empty in
  dp.(0).(1) <- IntSet.singleton 0;
  for i = 0 to h - 1 do
    for j = 0 to w - 1 do
      dp.(i + 1).(j + 1) <-
        IntSet.fold (fun sum s ->
          IntSet.add (abs (sum + ass.(i).(j) - bss.(i).(j))) @@
          IntSet.add (abs (sum - ass.(i).(j) + bss.(i).(j))) s) dp.(i).(j + 1) @@
        IntSet.fold (fun sum s ->
          IntSet.add (abs (sum + ass.(i).(j) - bss.(i).(j))) @@
          IntSet.add (abs (sum - ass.(i).(j) + bss.(i).(j))) s) dp.(i + 1).(j) IntSet.empty
    done
  done;
  Printf.printf "%d\n" @@
  IntSet.fold min dp.(h).(w) max_int

