#include<bits/stdc++.h>
using namespace std;

int adj[30][30];

int main() {
    cout.tie(nullptr)->sync_with_stdio(false);
    int N; cin >> N;
    vector<tuple<int, int, int>> points(N);
    for (int i = 0; i < N; ++i) {
        int a, b, c; cin >> a >> b >> c;
        --a, --b, --c;
        points[i] = make_tuple(a, b, c);
    }
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
            //if (i == j) continue;
            int ai, bi, ci, aj, bj, cj;
            tie(ai, bi, ci) = points[i];
            tie(aj, bj, cj) = points[j];
            adj[i][j] = abs(ai - aj) + abs(bi - bj) + max(0, cj - ci);
        }
    }
    vector<vector<int>> dp(1<<N, vector<int>(N, INT_MAX/2));
    for (int a = 1; a < N; ++a)
        dp[1 << a][a] = adj[0][a];
    for (int s = 0; s < (1<<N); s++)    //iterating through all subsets in lexicographical order
        for (int a = 0; a < N; a++) {
            int r = dp[s][a];
            for (int v = 0; v < N; v++) {   //looking through all transitions (cities to visit next)
                if (s & (1<<v)) continue;     //we cannot visit cities that are already visited
                int ns = s | (1<<v);          //perform transition
                int nr = r + adj[a][v];      //by adding edge (a - v) distance
                dp[ns][v] = min(dp[ns][v], nr); //relax result for state (ns,na) with nr
            }
        }
    cout << dp[(1 << N) - 1][0] << '\n';
}

//@formatter:off
#include<bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < int(n); ++i)
#define rrep(i,n) for (int i = int(n)-1; i >= 0; i--)
#define rep2(i,s,n) for (int i = int(s); i < int(n); ++i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vd vector<double>
#define vvd vector<vector<double>>
#define vs vector<string>
#define vc vector<char>
#define vvc vector<vector<char>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vp vector<P>
#define vvp vector<vector<P>>
using namespace std;
using ll = long long;
using P = pair<int,int>;
using LP = pair<ll,ll>;
template<class S,class T> istream& operator>>(istream &is,pair<S,T> &p) { return is >> p.first >> p.second; }
template<class S,class T> ostream& operator<<(ostream &os,const pair<S,T> &p) { return os<<'{'<<p.first<<","<<p.second<<'}'; }
template<class T> istream& operator>>(istream &is,vector<T> &v) { for(T &t:v){is>>t;} return is; }
template<class T> ostream& operator<<(ostream &os,const vector<T> &v) { os<<'[';rep(i,v.size())os<<v[i]<<(i==int(v.size()-1)?"":","); return os<<']'; }
void Yes(bool b) { cout << (b ? "Yes" : "No") << '\n'; }
void YES(bool b) { cout << (b ? "YES" : "NO") << '\n'; }
template<class T> bool chmin(T& a,T b) {if(a > b){a = b; return true;} return false;}
template<class T> bool chmax(T& a,T b) {if(a < b){a = b; return true;} return false;}
const int inf = 1001001001;
const ll linf = 1001001001001001001;
//@formatter:on

int dp[1 << 17][17];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n;
    cin >> n;
    vi x(n), y(n), z(n);
    rep(i, n) cin >> x[i] >> y[i] >> z[i];
    auto dist = [&](int i, int j) {
        return abs(x[i] - x[j]) + abs(y[i] - y[j]) + max(0, z[j] - z[i]);
    };
    rep(i, 1 << n) rep(j, n) dp[i][j] = inf;
    dp[1][0] = 0;
    int ans = inf;
    rep2(bit, 1, 1 << n) rep(j, n) {
            if (bit == (1 << n) - 1) {
                chmin(ans, dp[bit][j] + dist(j, 0));
            }
            rep(k, n) {
                if (bit >> k & 1) continue;
                chmin(dp[bit | 1 << k][k], dp[bit][j] + dist(j, k));
            }
        }
    cout << ans << endl;
}