# include "bits/stdc++.h"
using namespace std;
using LL = long long;
using ULL = unsigned long long;
const double PI = acos(-1);
template<class T>constexpr T INF() { return ::std::numeric_limits<T>::max(); }
template<class T>constexpr T HINF() { return INF<T>() / 2; }
template <typename T_char>T_char TL(T_char cX) { return tolower(cX); };
template <typename T_char>T_char TU(T_char cX) { return toupper(cX); };
typedef pair<LL, LL> pii;
const int vy[] = { -1, -1, -1, 0, 1, 1, 1, 0 }, vx[] = { -1, 0, 1, 1, 1, 0, -1, -1 };
const int dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 };
int popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++)if ((n >> i) & 1)cnt++; return cnt; }
int d_sum(LL n) { int ret = 0; while (n > 0) { ret += n % 10; n /= 10; }return ret; }
int d_cnt(LL n) { int ret = 0; while (n > 0) { ret++; n /= 10; }return ret; }
LL gcd(LL a, LL b) { if (b == 0)return a; return gcd(b, a%b); };
LL lcm(LL a, LL b) { LL g = gcd(a, b); return a / g*b; };
# define ALL(qpqpq)           (qpqpq).begin(),(qpqpq).end()
# define UNIQUE(wpwpw)        sort(ALL((wpwpw)));(wpwpw).erase(unique(ALL((wpwpw))),(wpwpw).end())
# define LOWER(epepe)         transform(ALL((epepe)),(epepe).begin(),TL<char>)
# define UPPER(rprpr)         transform(ALL((rprpr)),(rprpr).begin(),TU<char>)
# define FOR(i,tptpt,ypypy)   for(LL i=(tptpt);i<(ypypy);i++)
# define REP(i,upupu)         FOR(i,0,upupu)
# define INIT                 std::ios::sync_with_stdio(false);std::cin.tie(0)

using Flow = LL;
using Cost = LL;
const int MAX_V = 100000;

struct PrimalDual {
	struct Edge {
		LL d, c, f, w, r, is_r;
		Edge(LL d_, LL c_, LL f_, LL w_, LL r_, bool is_r_)
			: d(d_), c(c_), f(f_), w(w_), r(r_), is_r(is_r_) {}
	};
	LL n;
	vector<vector<Edge>> g;
	PrimalDual(LL n_) : n(n_), g(vector<vector<Edge> >(n_)) {}
	void add_edge(LL src, LL dst, LL cap, LL cost) {  // 有向辺
		LL rsrc = g[dst].size();
		LL rdst = g[src].size();
		g[src].emplace_back(dst, cap, 0, cost, rsrc, false);
		g[dst].emplace_back(src, cap, cap, -cost, rdst, true);
	}
	LL solve(LL s, LL t, LL f) {
		LL res = 0;
		static LL h[100000 + 10], dist[100000];
		static LL prevv[100000 + 10], preve[100000 + 10];
		priority_queue<pair<LL, LL>, vector<pair<LL, LL>>, greater<pair<LL, LL>> > q;
		fill(h, h + n, 0);
		while (f > 0) {
			fill(dist, dist + n, HINF<LL>());
			dist[s] = 0;
			q.emplace(0, s);
			while (q.size()) {
				LL cd;
				LL v;
				tie(cd, v) = q.top();
				q.pop();
				if (dist[v] < cd) continue;
				for (LL i = 0; i < (LL)(g[v].size()); ++i) {
					Edge &e = g[v][i];
					if (residue(e) == 0) continue;
					if (dist[e.d] + h[e.d] > cd + h[v] + e.w) {
						dist[e.d] = dist[v] + e.w + h[v] - h[e.d];
						prevv[e.d] = v;
						preve[e.d] = i;
						q.emplace(dist[e.d], e.d);
					}
				}
			}
			if (dist[t] == HINF<LL>()) return -1;  // 経路が見つからなかった
			// s-t 間最短路に沿って目一杯流す
			for (LL i = 0; i < n; ++i) h[i] += dist[i];
			LL d = f;
			for (LL v = t; v != s; v = prevv[v]) {
				d = min(d, residue(g[prevv[v]][preve[v]]));
			}
			f -= d;
			res += d * h[t];
			for (LL v = t; v != s; v = prevv[v]) {
				Edge &e = g[prevv[v]][preve[v]];
				e.f += d;
				g[v][e.r].f -= d;
			}
		}
		return res;
	}
	LL residue(const Edge &e) { return e.c - e.f; }
};
int n;
LL rx[1010], ry[1010], bx[1010], by[1010], rc[1010], bc[1010];
map<pair<int, int>, int> idxr, idxb;
int start = 0, goal = 1;
int type = 2;
int idx = 6;
LL sum;
int main(){
    cin >> n;
    REP(i, n){
        cin >> rx[i] >> ry[i] >> rc[i];
        sum += rc[i];
        if(idxr.find(make_pair(rx[i], ry[i])) == idxr.end()){
            idxr[make_pair(rx[i], ry[i])] = idx;
            idx++;
        }
    }
    REP(i, n){
        cin >> bx[i] >> by[i] >> bc[i];
        if(idxb.find(make_pair(bx[i], by[i])) == idxb.end()){
            idxb[make_pair(bx[i], by[i])] = idx;
            idx++;
        }
    }
    PrimalDual pd(idx);
    LL offset = 10000000000LL;
    REP(i, n){
        pd.add_edge(start, idxr[make_pair(rx[i], ry[i])], rc[i], 0);

        pd.add_edge(idxr[make_pair(rx[i], ry[i])], type + 0, HINF<LL>(), offset - (rx[i] + ry[i]));
        pd.add_edge(idxr[make_pair(rx[i], ry[i])], type + 1, HINF<LL>(), offset - (-rx[i] + ry[i]));
        pd.add_edge(idxr[make_pair(rx[i], ry[i])], type + 2, HINF<LL>(), offset - (rx[i] - ry[i]));
        pd.add_edge(idxr[make_pair(rx[i], ry[i])], type + 3, HINF<LL>(), offset - (-rx[i] - ry[i]));

        pd.add_edge(type + 0, idxb[make_pair(bx[i], by[i])], HINF<LL>(), offset - (-bx[i] - by[i]));
        pd.add_edge(type + 1, idxb[make_pair(bx[i], by[i])], HINF<LL>(), offset - (bx[i] - by[i]));
        pd.add_edge(type + 2, idxb[make_pair(bx[i], by[i])], HINF<LL>(), offset - (-bx[i] + by[i]));
        pd.add_edge(type + 3, idxb[make_pair(bx[i], by[i])], HINF<LL>(), offset - (bx[i] + by[i]));

        pd.add_edge(idxb[make_pair(bx[i], by[i])], goal, bc[i], 0);
    }
    cout << offset * sum * 2LL - pd.solve(start, goal, sum) << endl;
}

Measured memory_bytes: 2048