#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define sz(v) (int)v.size() 
#define all(v) v.begin(), v.end()
void dbg_out() { cerr << "\b\b]\n"; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T){ cerr << H << ", "; dbg_out(T...);}
#define watch(...) cerr << "[" << #__VA_ARGS__ << "]: [", dbg_out(__VA_ARGS__)


/****************************** CODE IS HERE ***********************************/


int main(){
    ios_base::sync_with_stdio(false); cin.tie(nullptr);

    int n, m; cin >> n >> m;
    vector <int> A(n*m);
    for (int &i: A) cin >> i;
    int mn = *min_element(all(A));
    ll ans = 0;
    for (int i: A) {
        ans += i - mn;
    }
    cout << ans;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
 
#define int long long int
#define double long double
#define pb push_back
#define fi first
#define se second
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pii>
#define pqi priority_queue<int>
#define test int tcase; cin>>tcase; for(int tc = 1; tc <= tcase; tc++)
#define inp(a,n,f) int a[n+f];for(int hh=f;hh<n+f;hh++)cin>>a[hh];
#define printdecimal(k) cout<<fixed<<setprecision(k);
#define mem(a,k) memset(a,k,sizeof(a))
#define ub upper_bound
#define lb lower_bound
#define mii map<int,int>
#define all(v) v.begin(),v.end()
#define mod (int)(1e9+7)
#define inf LLONG_MAX
int exp(int x,int y){int res=1;x=x%mod;while(y>0){if(y&1)res=(res*x)%mod;y=y>>1;x=(x*x)%mod;}return res;}
int modinv(int x){return exp(x,mod-2);}
int add(int a,int b){a%=mod,b%=mod;a=((a+b)%mod+mod)%mod;return a;}
int sub(int a,int b){a=((a-b)%mod+mod)%mod;return a;}
int mul(int a,int b){a%=mod,b%=mod;a=((a*b)%mod+mod)%mod;return a;}


signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n,m;
    cin>>n>>m;
    int mn=inf;
    int a[n][m];
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            cin>>a[i][j],mn=min(mn,a[i][j]);
    int ans=0;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            ans+=a[i][j]-mn;
    cout<<ans;
    return 0;
}   