#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define mp make_pair
#define pb push_back
#define int int64_t
#define rep(n) for(ll i=0;i<n;i++)
#define rrep(n) for(ll i=n-1;i>=0;i--)
#define inF freopen("input.in", "r", stdin);
#define outF freopen("output.in", "w", stdout);
#define endl '\n'
#define MOD 1000000007
#define mm(arr) memset(arr, 0, sizeof(arr))
#define F first
#define S second
#define scanArray(a,n) for(int i = 0; i < n; i++){cin >> a[i];}
#define coutArray(a,n) for(int i = 0; i < n; i++){cout << a[i] << " ";};cout << endl;
#define ld long double
#define PI 3.141592653589793238
#define srt(v) sort(v.begin(),v.end())
#define all(v) v.begin(), v.end()

int32_t main(){
    FAST
    int t=1;
    //cin >> t;
    while(t--){
        int n,m; cin >> n >> m;
        int a[n][m];
        int x = 1e9;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                cin >> a[i][j];
                x=min(x,a[i][j]);
            }
        }
        int sum=0;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                sum+=abs(a[i][j]-x);
            }
        }
        cout << sum << endl;
    }
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int h,w ;
    cin >> h >> w;
    vector<int> a(h*w);
    int m = 1000;
    for(int i =0; i<(h*w); i++)
    {
        cin >> a[i];
        if(m > a[i])
            m=a[i];
    }
    int t = 0;
    for(int i =0; i<(h*w); i++)
    {
        t+= a[i]-m;
    }
    cout << t << endl;
}


