/*
* @author:  codancer
* @createTime:  2020-10-17, 21:33:55
*/

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
const ll mod = 1e9+7;
#define pb push_back
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define fep(i,a,b) for(int i=(a);i>=(b);i--)
#define deb(x) cerr<<#x<<" = "<<(x)<<"\n"
typedef vector<int> VI;
typedef vector<ll> VII;
typedef pair<int,int> pii;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll Rand(ll B) {
	return (ull)rng() % B;
}

const int N = 20;

struct Point{
	ll x,y,z;
}p[N];
ll dis(Point a,Point b){
	return abs(a.x-b.x)+abs(a.y-b.y)+max(0LL,b.z-a.z);
}
ll dp[1<<17][17];//the ans of dp[status][last] 
int main(){
	for(int i=0;i<(1<<17);i++){
		for(int j=0;j<17;j++) dp[i][j]=1e18;
	}
	dp[1][0]=0;
	int n;
	cin>>n;
	rep(i,0,n-1){
		cin>>p[i].x>>p[i].y>>p[i].z;
	}

	for(int i=1;i<(1<<n);i++){
		for(int j=0;j<n;j++){
			if((i>>j)&1){
				for(int k=0;k<n;k++){
					if(!((i>>k)&1)){
						int nxt=(i|(1<<k));
						dp[nxt][k]=min(dp[nxt][k],dp[i][j]-dis(p[j],p[0])+dis(p[j],p[k])+dis(p[k],p[0]));
					}
				}
			} 
		}
	}

	ll ans=1e18;
	for(int i=0;i<n;i++) ans=min(ans,dp[(1<<n)-1][i]);
	cout<<ans<<endl;

	return 0;
}
#pragma GCC optimize("Ofast")
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <iomanip> // setprecision
#include <complex> // complex
#include <math.h> 
#include <functional>
#include <cassert>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
constexpr ll INF = 1e18;
constexpr ll LLMAX = 9223372036854775807;
constexpr int inf = 1e9;
constexpr ll mod = 1000000007;
// constexpr ll mod = 998244353;
const int dx[8] = {1, 0, -1, 0,1,1,-1,-1};
const int dy[8] = {0, 1, 0, -1,1,-1,1,-1};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
// ---------------------------------------------------------------------------


int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  vector<int> x(N),y(N),z(N);
  for(int i=0; i<N; i++){
    cin >> x[i] >> y[i] >> z[i];
  }
  vector<vector<int>> dp(N,vector<int>(1LL<<N,inf));
  dp[0][0] = 0;
  for(int bit=0; bit<(1LL<<N); bit++){
    for(int i=0; i<N; i++){
      for(int j=0; j<N; j++){
        chmin(dp[j][bit | (1LL<<j)],dp[i][bit] + abs(x[i]-x[j])+abs(y[i]-y[j])+max(0,z[j]-z[i]));
      }
    }
  }
  cout << dp[0][(1LL<<N)-1] << "\n";
  return 0;
}