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.init 6401 @@ fun _ ->
    Array.make_matrix (h + 1) (w + 1) false in
  dp.(0).(0).(1) <- true;
  for i = 0 to h - 1 do
    for j = 0 to w - 1 do
      for k = 0 to 6400 do
        let k' = abs (k + ass.(i).(j) - bss.(i).(j)) in
        if
          k' <= 6400 && (dp.(k).(i + 1).(j) || dp.(k).(i).(j + 1))
        then dp.(k').(i + 1).(j + 1) <- true;
        let k' = abs (k - ass.(i).(j) + bss.(i).(j)) in
        if
          k' <= 6400 && (dp.(k).(i + 1).(j) || dp.(k).(i).(j + 1))
        then dp.(k').(i + 1).(j + 1) <- true
      done
    done
  done;
  Printf.printf "%d\n" @@
  Array.fold_left min max_int @@
  Array.init 6401 @@ fun i ->
    if dp.(i).(h).(w) then i else max_int

