#include<bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define nl "\n"
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define Yes cout<<"YES\n"
#define No cout<<"NO\n"
#define yes cout<<"Yes\n"
#define no cout<<"No\n"
#define F first
#define sc second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define sortv(v) sort(v.begin(), v.end());
#define rsort(v) sort(v.rbegin(), v.rend());
#define revv(v) reverse(v.begin(), v.end());
const int N=1e5+9;
const int mod1=1e9+7, mod2=1e9+9;
const int base1=137, base2=277;
const long long int inf=1e9;
const long double eps=1e-9;
#define saiful_islam_bk
vector<vector<ll>>a(N, vector<ll>(5));
bool ok(vector<ll>&a, vector<ll>&b){
ll p=0, q=0;
for(ll i=0; i<a.size(); i++) p+=a[i];
for(ll i=0; i<b.size(); i++) q+=b[i];
if(p<q) return a<b;
else return a>b;
}
ll total(ll n, ll k){
ll d=0;
for(ll i=0; i<k; i++){
vector<ll>s(n);
for(ll j=0; j<n; j++) s[j]=a[j][i];
sort(all(s));
ll ps=0;
for(ll j=0; j<n; j++){
d+=s[j]*j-ps;
ps+=s[j];
}
}
return d;
}
void solve(){
ll n, k; cin>>n>>k;
for(ll i=0; i<n; i++){
for(ll j=0; j<k; j++){
cin>>a[i][j];
}
}
if(n<=1) return void(cout<<0<<nl);
sort(a.begin(), a.begin()+n, ok);
ll mn=LLONG_MAX;
for(ll i=0; i<n-1; i++){
ll p=0;
for(ll j=0; j<k; j++){
p+=abs(a[i][j]-a[i+1][j]);
}
mn=min(mn, p);
}
if(n==2) mn=0;
cout<<total(n, k)-mn<<nl;
// cout<<abs(a[0][0]-a[n-1][0])+abs(a[0][1]-a[n-1][1])<<nl;
// cout<<mn<<nl;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
saiful_islam_bk
ll test=1;
// cin>>test;
for(ll ii=1; ii<=test; ii++){
//cout<<"Case "<<ii<<": ";
solve();
}
}