#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define LOL cout << '\n';
#define sz(x) ((int)x.size())
#define INF LONG_LONG_MAX
#define NINF LONG_LONG_MIN
#define deb(x) cout << "[" << (#x) << "=" << x << "]" << '\n';
#define deb2(x,y) cout << "[" << (#x) << "=" << x << "] [" << (#y) << "=" << y << "]" << '\n';
#define deb3(x,y,z) cout << "[" << (#x) << "=" << x << "] [" << (#y) << "=" << y << "] [" << (#z) << "=" << z << "]" << '\n';

int n;
const int N=19;
int a[N][3];
int weight[N][N];
int dis[N][N];
int dp[N][1LL<<N];

int find(int cur,int mask){
	if(dp[cur][mask]!=-1) return dp[cur][mask];
	if(mask==(1LL<<(n))-1){
		//cout<<cur<<' '<<mask<<' '<<dis[cur][0];LOL
		return weight[cur][0];
	}
	int ans=INF;
	for(int i=0 ; i<n ; i++){
		if((mask|(1LL<<i))!=mask){
			ans=min(ans,weight[cur][i]+find(i,mask|(1LL<<i)));
            //cout<<cur<<" to "<<i<<" "<<(mask | (1<<(i)))<<" "<<ans<<"\n";
           // cout<<weight[cur][i]<<" "<<find(i, mask | (1<<(i)))<<"\n";
		}
	}
	return dp[cur][mask]=ans;
}

void solve(){
	cin>>n;
	for(int i=0 ; i<n ; i++){
		cin>>a[i][0]>>a[i][1]>>a[i][2];
	}
	for(int i=0 ; i<n ; i++){
		for(int j=0 ; j<n ; j++){
			weight[i][j]=abs(a[j][0]-a[i][0])+abs(a[j][1]-a[i][1])+max(0LL,a[j][2]-a[i][2]);
			dis[i][j]=weight[i][j];
		}
	}
	for(int k=0 ; k<n ; k++){
		for(int i=0 ; i<n ; i++){
			for(int j=0 ; j<n  ; j++){
				dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
			}
		}
	}
	//for(int i=0 ; i<n ; i++){
	//	for(int j=0 ; j<n ; j++){
	//		cout<<weight[i][j]<<' ';
	//	}LOL
	//}
	//LOL cout<<"-------"; LOL
	//for(int i=0 ; i<n ; i++){
	//	for(int j=0 ; j<n ; j++){
	//		cout<<dis[i][j]<<' ';
	//	}LOL
	//}
	//LOL cout<<"-------"; LOL
	memset(dp,-1,sizeof(dp));
	cout<<(find(0,0));LOL
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
  int _ = 1; //cin >> _;
  for(int i = 1 ; i <= _ ; i++){
    //cout << "Case #" << i << ": ";
    solve();
  }
  return 0;
}
#include <bits/stdc++.h>
#define ld long double
#define endl "\n"
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define ms(v,x) memset(v,x,sizeof(v))
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define per(i, a, b) for(int i = b-1; i>=a ; i--)
#define trav(a, x) for(auto& a : x)
#define allin(a , x) for(auto a : x)
#define Unique(v) sort(all(v));v.erase(unique(all(v)),v.end());
#define sz(v) ((int)v.size())
#define int long long
using namespace std;
typedef vector<int> vi;
#define y1 abacaba
#define left sefude
#define db(x) cerr << #x <<" == "<<x << endl;
#define db2(x,y) cerr<<#x <<" == "<<x<<", "<<#y<<" == "<<y<<endl;
#define db3(x,y,z) cerr << #x<<" == "<<x<<", "<<#y<<" == "<<y<<", "<<#z<<" == "<<z<<endl; 
#define prl cerr<<"called: "<< __LINE__ << endl;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
inline ll mod(ll n, ll m ){ ll ret = n%m; if(ret < 0) ret += m; return ret; }
ll gcd(ll a, ll b){return (b == 0LL ? a : gcd(b, a%b));}
ll exp(ll a,ll b,ll m){
    if(b==0LL) return 1LL;
    if(b==1LL) return mod(a,m);
    ll k = mod(exp(a,b/2,m),m);
    if(b&1LL){
        return mod(a*mod(k*k,m),m);
    }
    else return mod(k*k,m);
}
const int N = 17;
int x[N],y[N],z[N];
const int inf = 1e9;
int dist[N][(1<<N) + 10];
typedef array<int,3> ar;

int d(int i,int j){
  return abs(x[i] - x[j]) + abs(y[i] - y[j]) + max(0LL,z[j] - z[i]);
}

int32_t main(){
  fastio;
  rep(i,0,N){
    rep(j,0,(1<<N) + 10){
      dist[i][j] = inf;
    }
  }
  int n;
  cin >> n;
  rep(i,0,n){
    cin >> x[i] >> y[i] >> z[i];
  }

  priority_queue<ar,vector<ar>, greater<ar> > pq;
  dist[0][(1<<0)] = 0;
  pq.push({0,0,(1<<0)});
  while(!pq.empty()){
    ar Cur = pq.top();
    pq.pop();
    int cur = Cur[1];
    int msk = Cur[2];
    for(int j=0;j<n;j++){
      if(d(cur,j) + dist[cur][msk] < dist[j][msk|(1<<j)]){
        dist[j][msk|(1<<j)] = d(cur,j) + dist[cur][msk];
        pq.push({dist[j][msk|(1<<j)],j,(msk|(1<<j))});
      }
    }
  }

  cout << dist[0][(1<<n)-1]<<endl;


  // math -> gcd it all
  // Did u check N=1? Did you switch N,M?
}