import java.io._
import java.util.StringTokenizer
import scala.util.control.Breaks.{breakable,break}
import scala.collection.mutable.{Map, Stack, Queue, PriorityQueue, Set}

object Main extends App {
  val in = new InputReader(System.in)
  val pw = new PrintWriter(System.out)

  val H, W = in.next().toInt
  val A = Array.ofDim[Int](H+1, W+1)
  val B = Array.ofDim[Int](H+1, W+1)

  for (y <- 1 to H; x <- 1 to W) {
    A(y)(x) = in.next().toInt
  }
  for (y <- 1 to H; x <- 1 to W) {
    B(y)(x) = in.next().toInt
  }


  //
  val zero = (H+W - 1) * 80 // 12800
  val diff = zero * 2 + 1

  def toInd(p: (Int, Int)): Int = {
    val (x, y) = p
    (y-1) * W + (x-1)
  }
  val dp = Array.ofDim[Boolean](H*W, diff+1)

  val fuga = A(1)(1) - B(1)(1)
  dp(0)(zero + fuga) = true
  dp(0)(zero - fuga) = true

  for (y <- 1 to H) {
    for (x <- 1 to W) {
      val hoge = A(y)(x) - B(y)(x)
      if (y > 1) {
        for (n <- 0 to diff if dp(toInd((x, y-1)))(n)) {
          if (n + hoge <= diff && n + hoge >= 0)
            dp(toInd((x, y)))(n + hoge) = true
          if (n - hoge <= diff && n - hoge >= 0)
            dp(toInd((x, y)))(n - hoge) = true
        }
      }
      if (x > 1) {
        for (n <- 0 to diff if dp(toInd((x-1, y)))(n)) {
          if (n + hoge <= diff && n + hoge >= 0)
            dp(toInd((x, y)))(n + hoge) = true
          if (n - hoge <= diff && n - hoge >= 0)
            dp(toInd((x, y)))(n - hoge) = true
        }
      }
    }
  }

  var ans = -1
  val hoge = dp(toInd((W, H)))
  breakable {
    for (n <- 0 to 100000000) {
      if(hoge(zero - n) || hoge(zero + n)) {
        ans = n
        break
      }
    }
  }

  println(ans)
}

object Sub {
  def print2DimArray[A](mat: Array[Array[A]]): Unit = {
    mat.foreach(lin => println(lin.deep))
  }
}

class InputReader(stream: InputStream) {
  private[this] val reader = new BufferedReader(new InputStreamReader(stream))
  private[this] var tokenizer: StringTokenizer = new StringTokenizer(reader.readLine())
  def next(): String = {
    while (!tokenizer.hasMoreTokens()) {
      tokenizer = new StringTokenizer(reader.readLine())
    }
    tokenizer.nextToken()
  }
}
