#include <bits/stdc++.h>
// #include <atcoder/all>

using namespace std;
using ll = long long;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;
const int MOD = 1000000007;

template <typename T>
void print(const T &v)
{
    for (int i = 0; i < v.size(); i++)
    {
        if (i)
            cout << ' ';
        cout << v[i];
    }
    cout << endl;
}

int main()
{
    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<ll>> dp(n, vector<ll>((1 << n) + 10, LINF));
    dp[0][0] = 0;
    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)
                {
                    ll cost = dp[j][i - (1 << j)] + abs(x[k] - x[j]) + abs(y[k] - y[j]) + max(0, z[k] - z[j]);
                    dp[k][i] = min(dp[k][i], cost);
                }
            }
        }
    }
    cout << dp[0][(1 << n) - 1] << endl;
    return 0;
}
#include <bits/stdc++.h>

using namespace std;

mt19937 rng((int) chrono::steady_clock::now().time_since_epoch().count());

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n;
	cin >> n;
	vector<int> t(n), l(n), r(n);
	long long ans = 0;
	for (int i = 0; i < n; i++) {
		cin >> t[i] >> l[i] >> r[i];
		ans += i;
		for (int j = 0; j < i; j++) {
			if (t[i] == 1) {
				if (t[j] == 1) {
					if (r[j] < l[i] || l[j] > r[i])
						ans--;
				} else if (t[j] == 2) {
					if (r[j] <= l[i] || l[j] > r[i])
						ans--;
				} else if (t[j] == 3) {
					if (r[j] < l[i] || l[j] >= r[i])
						ans--;
				} else {
					if (r[j] <= l[i] || l[j] >= r[i])
						ans--;
				}
			} else if (t[i] == 2) {
				if (t[j] == 1) {
					if (r[j] < l[i] || l[j] >= r[i])
						ans--;
				} else if (t[j] == 2) {
					if (r[j] <= l[i] || l[j] >= r[i])
						ans--;
				} else if (t[j] == 3) {
					if (r[j] < l[i] || l[j] >= r[i])
						ans--;
				} else {
					if (r[j] <= l[i] || l[j] >= r[i])
						ans--;
				}
			} else if (t[i] == 3) {
				if (t[j] == 1) {
					if (r[j] <= l[i] || l[j] > r[i])
						ans--;
				} else if (t[j] == 2) {
					if (r[j] <= l[i] || l[j] > r[i])
						ans--;
				} else if (t[j] == 3) {
					if (r[j] <= l[i] || l[j] >= r[i])
						ans--;
				} else {
					if (r[j] <= l[i] || l[j] >= r[i])
						ans--;
				}
			} else {
				if (t[j] == 1) {
					if (r[j] <= l[i] || l[j] >= r[i])
						ans--;
				} else if (t[j] == 2) {
					if (r[j] <= l[i] || l[j] >= r[i])
						ans--;
				} else if (t[j] == 3) {
					if (r[j] <= l[i] || l[j] >= r[i])
						ans--;
				} else {
					if (r[j] <= l[i] || l[j] >= r[i])
						ans--;
				}
			}
		}
	}
	cout << ans << '\n';
	return 0;
}