let () =
    let main () =
        let h, w = Scanf.scanf "%d %d" (fun h w -> h, w) in
        let a = Array.init h (fun _ -> Array.init w (fun _ -> Scanf.scanf " %d" (fun n -> n))) in
        let b = Array.init h (fun _ -> Array.init w (fun _ -> Scanf.scanf " %d" (fun n -> n))) in
        let m = (h + w) * 120 in
        let m2 = m / 2 in
        let ar = Array.init (h + 1) (
            fun y -> Array.init (w + 1) (
                fun x -> Array.init m (
                    fun t -> t = m2 && (y = 0 || x = 0))))
        in
        for y = 1 to h do
            let y1 = ar.(y - 1) in
            let y0 = ar.(y) in
            for x = 1 to w do
                let x1 = y1.(x) in
                let x2 = y0.(x - 1) in
                let x0 = y0.(x) in
                let aa = a.(y - 1).(x - 1) - b.(y - 1).(x - 1) in
                for k = 0 to m - 1 do
                    if x1.(k) || x2.(k) then (
                        x0.(k + aa) <- true;
                        x0.(k - aa) <- true
                    )
                done
            done
        done;
        let rec loop i acc =
            if i = m then acc else
                let acc = if ar.(h).(w).(i) then min acc (abs (i - m2)) else acc in
                loop (i + 1) acc
        in
        let s = loop 0 max_int in
        Printf.printf "%d\n" s
    in
    main ()