#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,k;
const int M = 1e9 + 7;
int dp[1003][1003][2][2];
int func(int i, int ct, vector<vector<int>>&v, int prev1, int prev2){
if(i == v.size()){
if(ct == k){
return 0;
}
else{
return -1e12;
}
}
if(dp[i][ct][prev1][prev2] != -1){
return dp[i][ct][prev1][prev2];
}
int val = -1e12;
val = max(val, func(i+1,ct,v,0,0));
if(prev1 == 0 && prev2 == 0){
val = max(val, max(func(i+1,ct+1,v,0,0) + v[i][0] + v[i][1],func(i+1,ct+1,v,0,0) + v[i][1] + v[i][2]));
if(i+1<v.size()){
val = max(val, func(i+1,ct+2,v,1,0) + v[i][0] + v[i+1][0] + v[i][1] + v[i][2]);
val = max(val, func(i+1,ct+2,v,0,1) + v[i][0] + v[i+1][2] + v[i][1] + v[i][2]);
val = max(val, func(i+1,ct+2,v,1,1) + v[i][0] + v[i+1][0] + v[i][2] + v[i+1][2]);
val = max(val, func(i+1,ct+1,v,1,0) + v[i][0] + v[i+1][0]);
val = max(val, func(i+1,ct+1,v,0,1) + v[i][2] + v[i+1][2]);
}
}
else if(prev1 == 1 && prev2 == 0){
val = max(val, func(i+1,ct+1,v,0,0) + v[i][1] + v[i][2]);
if(i+1<v.size()){
val = max(val, func(i+1,ct+1,v,0,1) + v[i][2] + v[i+1][2]);
}
}
else if(prev1 == 0 && prev2 == 1){
val = max(val, func(i+1,ct+1,v,0,0) + v[i][0] + v[i][1]);
if(i+1<v.size()){
val = max(val, func(i+1,ct+1,v,1,0) + v[i][0] + v[i+1][0]);
}
}
return dp[i][ct][prev1][prev2] = val;
}
void ans(){
cin>>n>>k;
vector<vector<int>>v(n,vector<int>(3));
for(int i = 0; i<n; ++i){
for(int j =0; j<3; ++j){
cin>>v[i][j];
}
}
memset(dp,-1,sizeof(dp));
cout<<func(0,0,v,0,0)<<"\n";
}
int32_t main(){
ans();
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDs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