"use strict";
var GET=(function(){function f(s){return new g(s);}function g(s){this._s=s.trim().split("\n");this._y=0;}g.prototype.a=function(f){var s=this._s, y=this._y, r;if(typeof s[y]==="string")s[y]=s[y].split(" ").reverse();r=s[y].pop();if(!s[y].length)this._y++;return f?r:+r;};g.prototype.l=function(f){var s=this._s[this._y++].split(" ");return f?s:s.map(a=>+a);};g.prototype.m=function(n,f){var r=this._s.slice(this._y,this._y+=n).map(a=>a.split(" "));return f?r:r.map(a=>a.map(a=>+a));};g.prototype.r=function(n,f){var r=this._s.slice(this._y,this._y+=n);return f?r:r.map(a=>+a);};return f;})();
var o=GET(require("fs").readFileSync("/dev/stdin","utf8"));
function xArray(v){var a=arguments,l=a.length,r="Array(a["+--l+"]).fill(0).map(x=>{return "+v+";})";while(--l)r="Array(a["+l+"]).fill(0).map(x=>"+r+")";return eval(r);}
function abs(a){return Math.abs(a);}
function max(a,b){return Math.max(a,b);}

console.log(main());
function main(){
  var h = o.a();
  var w = o.a();
  var a = o.m(h);
  var b = o.m(h);
  var dp = [];
  var t = abs(a[0][0]-b[0][0]);
  dp[0] = Array(t+1);
  dp[0][t] = 1;
  var x,y;
  for(x = 1; x < w; x++){
    var u = dp[x-1].length;
    t = abs(a[0][x]-b[0][x]);
    dp[x] = Array(u+t);
    while(u--){
      if(dp[x-1][u] !== 1)continue;
      dp[x][u+t] = 1;
      if(t < u)dp[x][u-t] = 1;
      else dp[x][t-u] = 1;
    }
  }
  for(y = 1; y < h; y++){
    var dp2 = [];
    var u = dp[0].length;
    t = abs(a[y][0]-b[y][0]);
    dp2[0] = Array(u+t);
    while(u--){
      if(dp[0][u] !== 1)continue;
      dp2[0][u+t] = 1;
      if(t < u)dp2[0][u-t] = 1;
      else dp2[0][t-u] = 1;
    }
    for(x = 1; x < w; x++){
      var u = max(dp[x].length,dp2[x-1].length);
      t = abs(a[y][x]-b[y][x]);
      dp2[x] = Array(u+t);
      while(u--){
        if(dp[x][u] !== 1 && dp2[x-1][u] !== 1)continue;
        dp2[x][u+t] = 1;
        if(t < u)dp2[x][u-t] = 1;
        else dp2[x][t-u] = 1;
      }
    }
    dp = dp2;
  }
  for(var i = 0; 1; i++){
    if(dp[w-1][i])return i;
  }
}