#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;

typedef int _loop_int;
#define REP(i,n) for(_loop_int i=0;i<(_loop_int)(n);++i)
#define FOR(i,a,b) for(_loop_int i=(_loop_int)(a);i<(_loop_int)(b);++i)
#define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1;i>=(_loop_int)(a);--i)

#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG2(x,y) cout<<#x<<": "<<x<<" "<<#y<<": "<<y<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define DEBUG_ARR(v,n) cout<<#v<<":";REP(i,n)cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()

const ll MOD = 1000000007ll;
#define FIX(a) ((a)%MOD+MOD)%MOD

template<class T> inline bool chmin(T& a, T b) {
    if (a > b) { a = b; return true; }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) { a = b; return true; }
    return false;
}
template<class T>
void DEBUG_DP(T* viewdp, int ilen, int jlen){
    REP(i,ilen){
        REP(j,jlen){
            if(viewdp[i][j] != -1)
                printf("[% 4d]", viewdp[i][j]);
            else
                printf("[    ]");
        }
        puts("");
    }
}
const int N=81;
const int C=6400;
const int K=C*2+1;
int h,w;
int a[N][N], b[N][N];
bitset<K> dp[N][N];
int ans = INT_MAX;

int main() {
    ios::sync_with_stdio(false); cin.tie(0);

    REP(i,N){
        fill(a[i], a[i]+N, -1);
        fill(b[i], b[i]+N, -1);
    }

    cin>>h>>w;

    REP(i,h) REP(j,w) cin>>a[i][j];
    REP(i,h) REP(j,w) cin>>b[i][j];

    //int sx=0,sy=0, gx=w-1, gy=h-1;
    dp[0][0][abs(a[0][0]-b[0][0]) + 6400]=true;

    REP(i,h) REP(j,w){
        int ab =abs(a[i+1][j]-b[i+1][j]);

        dp[i+1][j] |= dp[i][j] << ab;
        dp[i+1][j] |= dp[i][j] >> ab;

        ab =abs(a[i][j+1]-b[i][j+1]);
        dp[i][j+1] |= dp[i][j] << ab;
        dp[i][j+1] |= dp[i][j] >> ab;
    }

    REP(i,K){
        if(dp[h-1][w-1][i]) chmin(ans, abs(6400-i));
    }

    cout << ans << endl;
    return 0;
}
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<bitset>
#include<deque>
#include<functional>
#include<iterator>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<utility>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
#define a first
#define b second
#define sz(x) (ll)((x).size())
#define pb push_back
#define mp make_pair
#define bg begin()
#define ed end()
#define all(x) (x).bg,(x).ed
#define rep(i,n) for(ll i=0;i<(n);i++)
#define rep1(i,n) for(ll i=1;i<=(n);i++)
#define rrep(i,n) for(ll i=(n)-1;i>=0;i--)
#define rrep1(i,n) for(ll i=(n);i>=1;i--)
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
const ll MOD=1000000007;
const ll INF=1000000000000000;
template<class T> inline bool chmin(T& a, T b){if(a>b){a=b;return true;}return false;}
template<class T> inline bool chmax(T& a, T b){if(a<b){a=b;return true;}return false;}
ll maxx(ll x,ll y,ll z){return max(max(x,y),z);}
ll minn(ll x,ll y,ll z){return min(min(x,y),z);}
ll gcd(ll x,ll y){if(x%y==0) return y;else return gcd(y,x%y);}
ll lcm(ll x,ll y){return x*(y/gcd(x,y));}
ll digsz(ll x){if(x==0) return 1;else{ll ans=0;while(x){x/=10;ans++;}return ans;}}
ll digsum(ll x){ll sum=0;while(x){sum+=x%10;x/=10;}return sum;}
vector<ll> pw2(62,1);vector<ll> pw10(19,1);


int main(){
    {rep1(i,61) pw2[i]=2*pw2[i-1];}
    {rep1(i,18) pw10[i]=10*pw10[i-1];}
    
    ll K,T; cin>>K>>T;
    vector<ll> A(T,0);
    rep(i,T){
        cin>>A[i];
    }
    sort(all(A));
    ll L=A[sz(A)-1];
    rep(i,sz(A)-1){
        L-=A[i];
    }
    cout<<max(L-1,0ll)<<endl;
}

