# include<bits/stdc++.h>
using namespace std;
int main()
{
  int m,n,i,j;
  cin>>m>>n;
  int a[m][n];
  int min=99999999;
  int res=0;
  for (i=0;i<m;i++)
  {
    for (j=0;j<n;j++)
    {
      cin>>a[i][j];
      if (a[i][j]<min){min=a[i][j];}
    }
  }
  for (i=0;i<m;i++)
  {
    for (j=0;j<n;j++)
    {
      res+=(a[i][j]-min);
    }
  }
  cout<<res;
  return 0;
}
      
#include <bits/stdc++.h>
#define task "triangles"
#define pb push_back
#define pll pair<ll, ll>
#define pii pair< ll, pll>
#define fi first
#define se second
using namespace std;
using ll = long long;
using ld = long double;

const int N = 555;
const ll mod = 1e15+7;
const ll ex = 1e5;

ll n, m, t, k, tong, a[N][N], b[N][N], T, ans, c[N][N], d[32][3], h[N], lo;
vector<ll> adj[N], kq;
string s, res;

void sol()
{
    cin >> n >> m;
    for(int i = 1; i <= n; i ++)
    {
        for(int j = 1; j < m; j ++)
        {
            cin >> a[i][j];
        }
    }
    for(int i = 1; i < n; i ++)
    {
        for(int j = 1; j <= m; j ++)
        {
            cin >> b[i][j];

        }
    }
    for(int i = 1; i <= n; i ++)for(int j = 1; j <= m; j ++)c[i][j] = mod;
    priority_queue< pii, vector<pii>, greater<pii> > pq;
    pii x;
    x.fi = 0;
    x.se.fi = 1;
    x.se.se = 1;
    pq.push(x);
    c[1][1] = 0;
    while(!pq.empty())
    {
        pii u = pq.top();
        pq.pop();
        if(u.fi != c[u.se.fi][u.se.se])continue;
        //cout << u.se.fi <<" " << u.se.se<<'\n';
        if(u.se.fi == n && u.se.se == m)
        {
            cout << u.fi;
            return;
        }
        if(u.se.se < m && c[u.se.fi][u.se.se + 1] > u.fi + a[u.se.fi][u.se.se])
        {
            c[u.se.fi][u.se.se + 1] = u.fi + a[u.se.fi][u.se.se];
            pii v;
            v.fi = u.fi + a[u.se.fi][u.se.se];
            v.se.fi = u.se.fi;
            v.se.se = u.se.se+1;
            pq.push(v);
        }
        if(u.se.se > 1 && c[u.se.fi][u.se.se - 1] > u.fi + a[u.se.fi][u.se.se-1])
        {
            c[u.se.fi][u.se.se - 1] = u.fi + a[u.se.fi][u.se.se-1];
            pii v;
            v.fi = u.fi + a[u.se.fi][u.se.se-1];
            v.se.fi = u.se.fi;
            v.se.se = u.se.se-1;
            pq.push(v);
        }
        if(u.se.fi < n && c[u.se.fi+1][u.se.se] > u.fi + b[u.se.fi][u.se.se])
        {
            c[u.se.fi+1][u.se.se] = u.fi + b[u.se.fi][u.se.se];
            pii v;
            v.fi = u.fi + b[u.se.fi][u.se.se];
            v.se.fi = u.se.fi+1;
            v.se.se = u.se.se;
            pq.push(v);
        }
        for(int i = 1;i < u.se.fi; i ++)
        {
            if(c[u.se.fi-i][u.se.se] > u.fi + i + 1)
            {
                c[u.se.fi-i][u.se.se] = u.fi + i + 1;
                pii v;
                v.fi = u.fi + i + 1;
                v.se.fi = u.se.fi - i;
                v.se.se = u.se.se;
                pq.push(v);
            }
        }
    }

}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    if (fopen(task".in", "r"))
        freopen(task".in", "r", stdin),
            freopen(task".out", "w", stdout);

    int ntest = 1;
    //cin >> ntest;
    while(ntest -- > 0)
    sol();
}
/*
3 4
1 (
3 )
2 )
3 )

-1 0 1 0 -1
*/
