#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ld long double
#define INF 1000000000000000000
typedef pair<ll, ll> pll;
typedef pair<int, int> pint;

template <class T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}

int V;
ll G[20][3] = {INF}; // グラフ
ll dp[150000][20];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    cin >> V;

    rep(i, V) {
        ll x, y, z;
        cin >> x >> y >> z;
        G[i][0] = x;
        G[i][1] = y;
        G[i][2] = z;
    }

    // dp の初期化
    rep(i, 150000) {
        rep(j, 20) { dp[i][j] = INF; }
    }
    dp[0][0] = 0; // スタートを頂点 0 とする
    ll a;

    rep(S, 1 << V) {
        rep(v, V) {
            rep(u, V) {
                if ((S & (1 << v)) == 0) { //Sにvが含まれないなら集合(S U v)を更新　集合への追加操作はS | (1 << v)
                    if (v != u)
                        dp[S | (1 << v)][v] =
                            min(dp[S | (1 << v)][v],
                                dp[S][u] + abs(G[v][0] - G[u][0]) +
                                    abs(G[v][1] - G[u][1]) +
                                    max(0LL, G[v][2] - G[u][2]));
                }
            }
        }
    }

    if (dp[(1 << V) - 1][0] != INF) {
        cout << dp[(1 << V) - 1][0] << endl;
    } else {
        cout << -1 << endl;
    }

    return 0;
}

# include <ciso646>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <map>
# include <queue>

namespace Main {
    namespace Source {
        typedef unsigned int uint;
        typedef long long unsigned int llu;
        template <const uint N>
        class Fenwick_tree {
            uint tree[N + 1];
            static inline const uint lowbit(const uint x) { return x bitand ~x + 1; }
        public:
            Fenwick_tree() { memset(tree, 0, sizeof tree); }
            inline const void add(uint x) { while (x <= N) ++tree[x], x += lowbit(x); }
            inline const uint query(uint x)
            { uint r(0); while (x) r += tree[x], x ^= lowbit(x); return r; }
        };
    }
    using namespace Source;
    enum { N = (const uint)2e5 };
    static uint n;
    static uint a[N + 1], b[N + 1];
    static llu sa, sb;
    static inline const bool mp(const uint a[N + 1], const uint b[N + 1]) {
        static uint t[N + 1], u[N + 1];
        for (register uint i(1); i <= n; ++i) t[i] = a[i], u[i] = b[i];
        std::sort(t + 1, t + n + 1), std::sort(u + 1, u + n + 1);
        for (register uint i(1); i <= n; ++i) if (t[i] not_eq u[i]) return false;
        return true;
    }
    static inline const void match(uint a[N + 1], const uint b[N + 1]) {
        static std::map<uint, uint> m;
        static std::queue<uint> p[N];
        for (register uint i(1); i <= n; ++i) if (not m.count(a[i])) m[a[i]] = m.size();
        for (register uint i(1); i <= n; ++i) p[m[b[i]]].push(i);
        for (register uint i(1); i <= n; ++i)
        { const uint t(m[a[i]]); a[i] = p[t].front(), p[t].pop(); }
    }
    static inline const llu inversion(const uint a[N + 1]) {
        Fenwick_tree<N> t; llu r(0);
        for (register uint i(n); i; --i) r += t.query(a[i] - 1), t.add(a[i]);
        return r;
    }
    static inline const void main() {
        scanf("%u", &n);
        for (register uint i(1); i <= n; ++i) scanf("%u", &a[i]), sa += a[i] += i;
        for (register uint i(1); i <= n; ++i) scanf("%u", &b[i]), sb += b[i] += i;
        if (not mp(a, b)) return (const void)puts("-1");
        match(a, b);
        printf("%llu\n", inversion(a));
    }
}

signed int main() { Main::main(); return 0; }