#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <utility>
#include <cstdarg>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
#include <set>
#include <map>

#define File
#define tab    char(9)
#define line   char(10)
#define space  char(32)
#define hell   int(998244353)
#define mod    int(1000000007)
#define MOD(x, y)     (((x) % (y) + (y)) % (y))
#define input_array(arr)  for(auto &_: arr) cin >> _
#define Saitama ios_base::sync_with_stdio(false); cin.tie(NULL)
#define rep(var, start, end) for(int var = start; var < end; var++)
#define output_array(arr) for(auto &_: arr) cout << _ << space; cout << line

#ifndef bug
#define debug(x)            cerr << '[' << #x << " = " << x << ']' << line   
#define debug2(x, y)        cerr << '[' << #x << " = " << x << ']' << space \
                                 << '[' << #y << " = " << y << ']' << line   
#define debug3(x, y, z)     cerr << '[' << #x << " = " << x << ']' << space \
                                 << '[' << #y << " = " << y << ']' << space \
                                 << '[' << #z << " = " << z << ']' << line   
#define debug4(x, y, z, _)  cerr << '[' << #x << " = " << x << ']' << space \
                                 << '[' << #y << " = " << y << ']' << space \
                                 << '[' << #z << " = " << z << ']' << space \
                                 << '[' << #_ << " = " << _ << ']' << line	 
#define bug
#endif
			      
typedef long long ll;   // use %lld
typedef long double ld; // use %lf

using namespace std;

vector<ll> prime_factors(ll num)
{
	vector<ll> ans;
	
	// 1 is not included in the implementation
	while(num % 2){
		ans.push_back(2);
		num /= 2;
	}
	
	for(int i = 3; i * i <= num; i += 2){
		while(num % i == 0){
			ans.push_back(i);
			num /= i;
		}
	}
	
	// if the number was prime (greater than 2)
	if(num > 2)
		ans.push_back(num);
		
	return ans;
}

const ld pi = 3.14159265358979323846;

void tests()
{
	int rows, cols;
	cin >> rows >> cols;
	
	vector<int> arr(rows * cols);
	input_array(arr);
	
	int minimum = *min_element(arr.begin(), arr.end());
	
	ll ans = 0;
	
	rep(i, 0, rows * cols){
		ans += arr[i] - minimum;
	}

	cout << ans << line;
}

int32_t main()
{
	Saitama;
	
	#ifndef File
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#endif

	int t = 1;
	// cin >> t;

	while(t--){
		tests();
	}

	return 0;
}

/*NEVER GIVE UP*/
/*First try brute force and check if it is in the range of time complexity*/
/*Then try to solve the problem in mathematical form*/
/*Then try to imagine how the final ans will look like*/
/*Write on paper if still not able to get*/
/*Try to think diff. algorithms and data structures that could solve the problem*/
#include <bits/stdc++.h>
using namespace std;
int main()
{long long r=0,c=0,s=0,min=1000;
    cin>>r>>c;
    long long a[r][c];
    for(long long i=0;i<r;i++)
    {
        for(long long j=0;j<c;j++)
        {
            cin>>a[i][j];
            if(a[i][j]<min)
            min=a[i][j];
        }
    }
    for(long long i=0;i<r;i++)
    {
        for(long long j=0;j<c;j++)
        s+=(a[i][j]-min);
    }
    cout<<s;
}