import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.bitmanip;
import std.bigint;

BigInt[] tbl;
static this () {
  tbl = new BigInt[100];
  tbl[0] = BigInt (1);
  foreach (i; 1 .. tbl.length) {
    tbl[i] = tbl[i-1] << 1;
  }
}

BigInt go (const ref BigInt a, int p) {
  BigInt b = a;
  b <<= p;
  b |= a >> p;
  foreach (i; 0 .. p + 1) {
    if ((tbl[i] & a) != 0) {
      b |= tbl[p - i];
    }
  }
  return b;
}

void main() {
  int h, w;
  readf (" %d %d", &h, &w);
  auto a = new int[][](h, w);
  auto b = new int[][](h, w);
  foreach (j; 0 .. h) foreach (i; 0 .. w) {
    readf (" %d", &a[j][i]);
  }
  foreach (j; 0 .. h) foreach (i; 0 .. w) {
    readf (" %d", &b[j][i]);
  }
  int d (int j, int i) {
    return abs (a[j][i] - b[j][i]);
  }
  auto c = new BigInt[w];
  c[0] = BigInt (1);
  c[0] <<= d (0, 0);
  foreach (i; 1 .. w) {
    c[i] = go (c[i-1], d (0, i));  
  }
  foreach (j; 1 .. h) {
    auto e = new BigInt[w];
    e[0] = go (c[0], d (j, 0));
    foreach (i; 1 .. w) {
      int p = d (j, i);
      auto tmp = e[i - 1] | c[i];
      e[i] = go (tmp, p); 
    }
    c = e;
  }
  auto x = c[w - 1];
  foreach (k; 0 .. int.max) {
    if ((x & tbl[0]) != 0) {
      writeln (k);
      break;
    }
    x >>= 1;
  }
}

