#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define int long long
#define ld long double
#define f(i,x,n) for(int i=x;i<n;i++)
using namespace std;
const int mod=1e9+7;
const int inf=1e18;
int n,x[20],y[20],z[20];
int dp[200000][20];
int dfs(int mask,int pos) {
	int &an=dp[mask][pos];
	// cout<<mask<<endl;
	if(an!=-1)
		return an;
	if(mask==((1ll<<n)-1)) {
		return abs(x[0]-x[pos])+abs(y[0]-y[pos])+max(0ll,z[0]-z[pos]);
	}
	an=inf;
	f(i,0,n) {
		if((mask&(1ll<<i)) ==0 ) {
		// cout<<i<<endl;
			an=min(an,abs(x[pos]-x[i])+abs(y[pos]-y[i])+max(0ll,z[i]-z[pos])+dfs(mask|(1ll<<i),i) );
		}
	}
	return an;
}
void solve() {
	f(i,0,200000)
		f(j,0,18)	
			dp[i][j]=-1;
	cin>>n;
	f(i,0,n) {
		cin>>x[i]>>y[i]>>z[i];
	}
	cout<<dfs(1,0)<<'\n';
}

int32_t main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);
	int t=1;
	// cin>>t;
	while(t--) {
		solve();
	}
	return 0;
}
#include<bits/stdc++.h>
#define int long long
#define ld long double
#define fi first
#define se second
#define vll vector<int>
#define pii pair<int,int>
#define pb push_back
#define sz(v) (int)(v).size()
#define inf (int)(1e18)
#define md (int)(998244353)
#define all(v) (v).begin(),(v).end()
#define rep(i,a,b) for(int i=a;i<b;++i)
using namespace std;

const int M = 3e5 + 10;

int ex(int a,int b=md-2){
	int ans=1;
	while(b) {
		if(b&1) ans=(ans*a)%md;
		a=(a*a)%md,b/=2;
	}
	return ans;
} 
inline int add(int a,int b){
	a+=b;
	if(a>=md) a-=md;
	return a;
}
inline int sub(int a,int b){
	a-=b;
	if(a<0) a+=md;
	return a;
}
inline int mul(int a,int b){
	return (a*b)%md;
}

int f[M],iv[M];
int C(int n,int r){
	if(n<r) return 0;
	return mul(f[n],mul(iv[r],iv[n-r]));
}

void init(){
	f[0]=1;
	for(int i=1;i<M;++i) {
		f[i]=mul(i,f[i-1]);
	}
	iv[M-1]=ex(f[M-1]);
	for(int i=M-2;i>=0;--i) {
		iv[i]=mul(i+1,iv[i+1]);
	}
}

int spf[M];

int get(int n,int x) {
	int ans=1;
	while(x>1) {
		int p=spf[x],cnt=0;
		while(x%p==0) x/=p,++cnt;
		ans=mul(ans,C(cnt+n-1,n-1));
	}
	return ans;
}

int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
   	
   	init();
   	
   	spf[1]=1;
   	rep(i,2,M) if(!spf[i])
   	for(int j=i;j<M;j+=i) if(!spf[j]) spf[j]=i;
   	
   	int n,m;
   	cin>>n>>m;
   	int ans=0;
   	rep(i,1,m+1) ans=add(ans,get(n,i));
   	cout<<ans;
   	
   	
    return 0;
}