function main(input) {
  var H = input[0].split(" ")[0]-0;
  var W = input[0].split(" ")[1]-0;
  var amap = [];
  var bmap = [];
  for(var i=0;i<H;i++){
    amap.push(input[i+1].split(" ").map(v=>v-0));
    bmap.push(input[i+H+1].split(" ").map(v=>v-0));
  }

  var dp = [];
  for(var h=0;h<H;h++){
    dp[h] = [];
    for(var w=0;w<W;w++){
      dp[h][w] = {};
      var dis = Math.abs(amap[h][w]-bmap[h][w]);
      if(h==0&&w==0){
        dp[h][w][dis]=true;
      }else{
        if(h>0){
          var map = dp[h-1][w];
          Object.keys(map).forEach(k=>{
            k=k-0;
            dp[h][w][Math.abs(k-dis)] = true;
            dp[h][w][Math.abs(k+dis)] = true;
          });
        }
        if(w>0){
          var map = dp[h][w-1];
          Object.keys(map).forEach(k=>{
            k=k-0;
            dp[h][w][Math.abs(k-dis)] = true;
            dp[h][w][Math.abs(k+dis)] = true;
          });
        }
      }
    }
  }
  console.log(Object.keys(dp[H-1][W-1]).sort((a,b)=>a-b)[0]);
}

main(require('fs').readFileSync('/dev/stdin', 'utf8').trim().split("\n"))
