(* Examples *)
(*
stdin.ReadLine()
printfn "%d"
printfn "%s"
*)

(* Library *)
module Library = ()

(* Standard Input *)
let readSplit elementFun=
  stdin.ReadLine().Split()
  |> Array.map elementFun

let read elementFun =
  stdin.ReadLine() |> elementFun

let readMap H =
  [|for i in 1..H -> stdin.ReadLine()|]
  |> Array.map(fun line ->
    line.ToCharArray()
  )
  |> array2D

let readSplitMap H func =
  [|for i in 1..H -> readSplit func|]
  |> array2D

(* Main *)
open Library

let INF = 1 <<< 30

let [|H; W|] = readSplit int

let mapA = readSplitMap H int
let mapB = readSplitMap H int

let absMap = 
  Array2D.create H W 0
  |> Array2D.mapi(fun i j _ ->
    abs (int mapA.[i, j] - int mapB.[i, j])
  )

let solveMap =
  Array2D.create H W 0

[|0..H - 1|]
|>Array.iter (fun i ->
  [|0..W - 1|]
  |>Array.iter (fun j ->
    if i = 0 && j = 0 then 
      solveMap.[0, 0] <- absMap.[0, 0]
    else
      solveMap.[i, j] <- min 
        (if i <> 0 then abs(solveMap.[i - 1, j] - absMap.[i, j]) else INF)
        (if j <> 0 then abs(solveMap.[i, j - 1] - absMap.[i, j]) else INF)          
  )
)

solveMap.[H - 1, W - 1] |> printfn "%d"