#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <cmath>
#include <random>
#include <tuple>
#include <numeric>
#include <map>
#include <iomanip>
#include <cassert>
#include <bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int mod = 1e9 + 7;
template <typename T, typename = void>
struct has_val_function : std::false_type {};
template <typename T>
struct has_val_function<T, std::void_t<decltype(std::declval<std::ostream&>() << std::declval<T>().val())>> : std::true_type { };
template<typename t>
typename std::enable_if_t<has_val_function<t>::value, std::ostream&>
operator << (ostream& o, const t &c)
{
o << c.val() << " ";
return o;
}
template<typename t, typename b>
ostream& operator << (ostream& o, const pair<t, b> &c)
{
o << "(" << c.first <<"," << c.second << ") ";
return o;
}
template<typename t, typename b, typename x>
ostream& operator << (ostream& o, const tuple<t, b, x> &c)
{
o << "(" << std::get<0>(c) <<"," << std::get<1>(c) << "," << std::get<2>(c) << ") ";
return o;
}
template<typename t>
typename std::enable_if_t<std::is_void<std::void_t<decltype(std::begin(std::declval<t>())), decltype(std::end(std::declval<t>()))>>::value, std::ostream&>
operator << (ostream& o, const t &c)
{
for(size_t i = 0; i < c.size(); i++)
{
o << c[i] << " ";
}
return o;
}
const int N = 2e5 + 5;
const string yes = "YES";
const string no = "NO";
int a[N][5];
int n, k;
int main()
{
#ifdef local
freopen("in.txt", "r", stdin);
#endif // local
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for(int i = 1; i <= n; i++) {
for(int j = 0; j < k; j++) {
cin >> a[i][j];
}
}
vector<set<pair<int, int>>> closed(1 << k), open(1 << k);
auto insert = [&](auto & vec_s, int id) {
int ans = 0;
for(int i = 0; i < (1 << k); i++) {
for(int j = 0; j < k; j++) {
if (i >> j & 1) {
ans += a[id][j];
}
else ans -= a[id][j];
}
vec_s[i].insert({ans, id});
}
};
auto remove = [&](auto & vec_s, int id) {
int ans = 0;
for(int i = 0; i < (1 << k); i++) {
for(int j = 0; j < k; j++) {
if (i >> j & 1) {
ans += a[id][j];
}
else ans -= a[id][j];
}
vec_s[i].erase({ans, id});
}
};
for(int i = 1; i <= n; i++) {
if (i == 1) {
insert(closed, i);
}
else insert(open, i);
}
ll ans = 0;
for(int i = 1; i < n; i++) {
int best = 0;
int id = 0;
for(int j = 0; j < (1 << k); j++) {
auto minv = *closed[j].begin();
auto maxv = *closed[j].rbegin();
auto cminv = *open[j].begin();
auto cmaxv = *open[j].rbegin();
if (maxv.first - cminv.first >= best) {
best = maxv.first - cminv.first;
id = cminv.second;
}
if (cmaxv.first - minv.first >= best) {
best = cmaxv.first - minv.first;
id = cmaxv.second;
}
}
ans += best;
insert(closed, id);
remove(open, id);
}
cout << ans << endl;
}