#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>
#include<cstdio>
#include<map>
#include<cassert>

using namespace std;

#define ll long long
#define reps(i, a, b) for(int i = a; i < b; i++)
#define rreps(i, a, b) for(int i = a-1; i >= b; i--)
#define rep(i, n) reps(i, 0, n)
#define rrep(i, n) rreps(i, n, 0)
#define P pair<int, int>
#define vec vector<int>
#define mat vector<vec>

const ll mod = 1000000007;
const int INF = 1001001001;

int main(){
    int n;
    cin >> n;
    mat x(n, vec(3));
    mat dist(n, vec(n));
    rep(i, n){
        rep(j, 3) cin >> x[i][j];
    }
    rep(i, n){
        rep(j, n){
            dist[i][j] = abs(x[i][0]-x[j][0]) + abs(x[i][1]-x[j][1]) + max(0, x[j][2]-x[i][2]);
        }
    }
    mat dp(1<<n, vec(n, INF));
    reps(i, 1, n){
        dp[(1<<i)][i] = dist[0][i];
    }
    rep(i, 1<<n){
        rep(j, n){
            rep(k, n){
                if((i>>k)&1){
                    dp[i|(1<<j)][j] = min(dp[i][k]+dist[k][j], dp[i|(1<<j)][j]);
                }
            }
        }
    }
    // rep(i, 1<<n){
    //     rep(j, n){
    //         cout << dp[i][j] << " ";
    //     }cout << endl;
    // }

    cout << dp[(1<<n)-1][0] << endl;
}
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
using namespace std;
constexpr int kMod = 1000000007;
typedef long long LL;
typedef tuple<LL, LL, LL> TP;
constexpr LL kInf = 1e18;

template<typename T>
auto makeNdVector(int n, const T& v) {
  return vector<T>(n, v);
}

template<typename T, typename N, typename... Tail>
auto makeNdVector(const N& n, const Tail&... tail) {
  auto vec = makeNdVector<T>(tail...);
  vector<decltype(vec)> vecs(n, vec);
  return vecs;
}

void chmin(LL& a, LL b) { a = min(a, b); }
LL dist(TP t1, TP t2) {
  auto [x1, y1, z1] = t1;
  auto [x2, y2, z2] = t2;
  return abs(x2 - x1) + abs(y2 - y1) + max(0LL, z2 - z1);
}

int main() {
  int N; cin >> N;
  vector<TP> tps(N);
  for (int i = 0; i < N; ++i) {
    int x, y, z; cin >> x >> y >> z;
    tps[i] = {x, y, z};
  }

  auto dp = makeNdVector<LL>(1 << N, N, kInf);
  dp[1][0] = 0;

  for (int S = 0; S < (1 << N); ++S) {
    bitset<18> b(S);
    for (int i = 0; i < N; ++i) {
      if (b.test(i)) {
        for (int j = 0; j < N; ++j) {
          LL d = dist(tps[i], tps[j]);
          chmin(dp[S | (1 << j)][j], dp[S][i] + d);
        }
      }
    }
  }

  cout << dp[(1 << N) - 1][0] << endl;
}
