#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<algorithm>
#define FOR(i,a,b) for(i=(a);i<=(b);i++)
#define ROF(i,a,b) for(i=(a);i>=(b);i--)
#define pb push_back
#define mp make_pair
#define N 500010
using namespace std;
typedef long long LL;
typedef long double LD;
struct poi{int x,y,w;}a[N],b[N];
bool cmp(poi a,poi b){return a.x<b.x||(a.x==b.x&&a.y<b.y);}
int n,m,dp[N],c[N];
int main()
{
ios::sync_with_stdio(false);
scanf("%d%d",&n,&m);int i,j;
FOR(i,1,n) scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].w);
sort(a+1,a+1+n,cmp);memset(dp,235,sizeof(dp));
dp[1]=a[1].w;c[1]=1;
FOR(i,2,n)
{
int Y=a[i].y;int Max=-1e9;
FOR(j,1,Y)
{
int tmp=dp[j]-(a[i].x-c[j])*(a[i].x-c[j])-(Y-j)*(Y-j);
if (tmp>Max) Max=tmp;
}
Max+=a[i].w;
c[Y]=a[i].x;dp[Y]=Max;
}
printf("%d\n",dp[m]);
return 0;
}