#include<bits/stdc++.h>
#define ll long long
#define inf 1000000010
#define N 2010
#define red(x) (x)
#define blue(x) (n+(x))
#define id(x) (2002+(x))
using namespace std;
const ll mod=1e9+7;
ll S,T;
ll read()
{
    ll x=0,f=1;
    char c=getchar();
    while (c<'0'||c>'9')
    {
        if (c=='-') f=-1;
        c=getchar();
    }
    while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
    return x*f;
}
ll n,p[N],d[N],q[N],pre[N],t=-1,ans;
bool flag[N];
ll inc(ll &x)
{
    x++;
    if (x>2007) x-=2007;
    return x;
}
struct edge
{
    ll to,nxt,cap,flow,cost;
} s[N<<5];
void add(ll x,ll y,ll z,ll cost)
{
    s[++t].to=y,s[t].nxt=p[x],s[t].cap=z,s[t].flow=0,s[t].cost=cost,p[x]=t;
    s[++t].to=x,s[t].nxt=p[y],s[t].cap=0,s[t].flow=0,s[t].cost=-cost,p[y]=t;
}
bool spfa()
{
    memset(d,60,sizeof(d));
    d[S]=0;
    memset(flag,0,sizeof(flag));
    ll head=0,tail=1;
    q[1]=S;
    do
    {
        ll x=q[inc(head)];
        flag[x]=0;
        for (ll i=p[x]; ~i; i=s[i].nxt)
            if (s[i].flow<s[i].cap&&d[x]+s[i].cost<d[s[i].to])
            {
                d[s[i].to]=d[x]+s[i].cost;
                pre[s[i].to]=i;
                if (!flag[s[i].to]) q[inc(tail)]=s[i].to,flag[s[i].to]=1;
            }
    }
    while (head!=tail);
    return d[T]<10000000000000000ll;
}
void ekspfa()
{
    while (spfa())
    {
        ll v=n;
        for (ll i=T; i!=S; i=s[pre[i]^1].to)
            v=min(v,s[pre[i]].cap-s[pre[i]].flow);
        for (ll i=T; i!=S; i=s[pre[i]^1].to)
            s[pre[i]].flow+=v,s[pre[i]^1].flow-=v,ans+=v*s[pre[i]].cost;
    }
}
ll val[10];
struct node
{
    ll x,y,z;
    ll tmp[10];
} a[N],b[N];
int main()
{
    n=read();
    memset(p,255,sizeof(p));
    S=0,T=2*n+1;
    for(ll i=1; i<=n; i++)
    {
        a[i].x=read(),a[i].y=read(),a[i].z=read();
        //readf("%d%d%d",&a[i].x,&a[i].y,&a[i].z);
        //cin>>a[i].x>>a[i].y>>a[i].z;
        add(S,i,a[i].z,0);
        a[i].tmp[0]=a[i].x+a[i].y;
        a[i].tmp[1]=a[i].x-a[i].y;
        a[i].tmp[2]=-a[i].x+a[i].y;
        a[i].tmp[3]=-a[i].x-a[i].y;
    }
    for(ll i=1; i<=n; i++)
    {
        b[i].x=read(),b[i].y=read(),b[i].z=read();
        //readf("%d%d%d",&b[i].x,&b[i].y,&b[i].z);
        add(i+n,T,b[i].z,0);
        b[i].tmp[0]=-b[i].x-b[i].y;
        b[i].tmp[1]=-b[i].x+b[i].y;
        b[i].tmp[2]=b[i].x-b[i].y;
        b[i].tmp[3]=b[i].x+b[i].y;
    }
    for(ll i=1; i<=4; i++)
    {
        val[i-1]=2*n+i+1;
    }
    for(ll i=1; i<=n; i++)
    {
        for(ll j=0; j<4; j++)
        {
            add(i,val[j],a[i].z,-a[i].tmp[j]);
            add(val[j],i+n,b[i].z,-b[i].tmp[j]);
        }
    }
    ekspfa();
    cout<<-ans<<endl;
    return 0;
}

### Prompt

Construct a CPP code solution to the problem outlined:

Snuke is playing with red and blue balls, placing them on a two-dimensional plane.

First, he performed N operations to place red balls. In the i-th of these operations, he placed RC_i red balls at coordinates (RX_i,RY_i). Then, he performed another N operations to place blue balls. In the i-th of these operations, he placed BC_i blue balls at coordinates (BX_i,BY_i). The total number of red balls placed and the total number of blue balls placed are equal, that is, \sum_{i=1}^{N} RC_i = \sum_{i=1}^{N} BC_i. Let this value be S.

Snuke will now form S pairs of red and blue balls so that every ball belongs to exactly one pair. Let us define the score of a pair of a red ball at coordinates (rx, ry) and a blue ball at coordinates (bx, by) as |rx-bx| + |ry-by|.

Snuke wants to maximize the sum of the scores of the pairs. Help him by finding the maximum possible sum of the scores of the pairs.

Constraints

* 1 \leq N \leq 1000
* 0 \leq RX_i,RY_i,BX_i,BY_i \leq 10^9
* 1 \leq RC_i,BC_i \leq 10
* \sum_{i=1}^{N} RC_i = \sum_{i=1}^{N} BC_i
* All values in input are integers.

Input

Input is given from Standard Input in the following format:


N
RX_1 RY_1 RC_1
RX_2 RY_2 RC_2
\vdots
RX_N RY_N RC_N
BX_1 BY_1 BC_1
BX_2 BY_2 BC_2
\vdots
BX_N BY_N BC_N


Output

Print the maximum possible sum of the scores of the pairs.

Examples

Input

2
0 0 1
3 2 1
2 2 1
5 0 1


Output

8


Input

3
0 0 1
2 2 1
0 0 2
1 1 1
1 1 1
3 3 2


Output

16


Input

10
582463373 690528069 8
621230322 318051944 4
356524296 974059503 6
372751381 111542460 9
392867214 581476334 6
606955458 513028121 5
882201596 791660614 9
250465517 91918758 3
618624774 406956634 6
426294747 736401096 5
974896051 888765942 5
726682138 336960821 3
715144179 82444709 6
599055841 501257806 6
390484433 962747856 4
912334580 219343832 8
570458984 648862300 6
638017635 572157978 10
435958984 585073520 7
445612658 234265014 6


Output

45152033546

### Response

```cpp
#include<bits/stdc++.h>
#define ll long long
#define inf 1000000010
#define N 2010
#define red(x) (x)
#define blue(x) (n+(x))
#define id(x) (2002+(x))
using namespace std;
const ll mod=1e9+7;
ll S,T;
ll read()
{
    ll x=0,f=1;
    char c=getchar();
    while (c<'0'||c>'9')
    {
        if (c=='-') f=-1;
        c=getchar();
    }
    while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
    return x*f;
}
ll n,p[N],d[N],q[N],pre[N],t=-1,ans;
bool flag[N];
ll inc(ll &x)
{
    x++;
    if (x>2007) x-=2007;
    return x;
}
struct edge
{
    ll to,nxt,cap,flow,cost;
} s[N<<5];
void add(ll x,ll y,ll z,ll cost)
{
    s[++t].to=y,s[t].nxt=p[x],s[t].cap=z,s[t].flow=0,s[t].cost=cost,p[x]=t;
    s[++t].to=x,s[t].nxt=p[y],s[t].cap=0,s[t].flow=0,s[t].cost=-cost,p[y]=t;
}
bool spfa()
{
    memset(d,60,sizeof(d));
    d[S]=0;
    memset(flag,0,sizeof(flag));
    ll head=0,tail=1;
    q[1]=S;
    do
    {
        ll x=q[inc(head)];
        flag[x]=0;
        for (ll i=p[x]; ~i; i=s[i].nxt)
            if (s[i].flow<s[i].cap&&d[x]+s[i].cost<d[s[i].to])
            {
                d[s[i].to]=d[x]+s[i].cost;
                pre[s[i].to]=i;
                if (!flag[s[i].to]) q[inc(tail)]=s[i].to,flag[s[i].to]=1;
            }
    }
    while (head!=tail);
    return d[T]<10000000000000000ll;
}
void ekspfa()
{
    while (spfa())
    {
        ll v=n;
        for (ll i=T; i!=S; i=s[pre[i]^1].to)
            v=min(v,s[pre[i]].cap-s[pre[i]].flow);
        for (ll i=T; i!=S; i=s[pre[i]^1].to)
            s[pre[i]].flow+=v,s[pre[i]^1].flow-=v,ans+=v*s[pre[i]].cost;
    }
}
ll val[10];
struct node
{
    ll x,y,z;
    ll tmp[10];
} a[N],b[N];
int main()
{
    n=read();
    memset(p,255,sizeof(p));
    S=0,T=2*n+1;
    for(ll i=1; i<=n; i++)
    {
        a[i].x=read(),a[i].y=read(),a[i].z=read();
        //readf("%d%d%d",&a[i].x,&a[i].y,&a[i].z);
        //cin>>a[i].x>>a[i].y>>a[i].z;
        add(S,i,a[i].z,0);
        a[i].tmp[0]=a[i].x+a[i].y;
        a[i].tmp[1]=a[i].x-a[i].y;
        a[i].tmp[2]=-a[i].x+a[i].y;
        a[i].tmp[3]=-a[i].x-a[i].y;
    }
    for(ll i=1; i<=n; i++)
    {
        b[i].x=read(),b[i].y=read(),b[i].z=read();
        //readf("%d%d%d",&b[i].x,&b[i].y,&b[i].z);
        add(i+n,T,b[i].z,0);
        b[i].tmp[0]=-b[i].x-b[i].y;
        b[i].tmp[1]=-b[i].x+b[i].y;
        b[i].tmp[2]=b[i].x-b[i].y;
        b[i].tmp[3]=b[i].x+b[i].y;
    }
    for(ll i=1; i<=4; i++)
    {
        val[i-1]=2*n+i+1;
    }
    for(ll i=1; i<=n; i++)
    {
        for(ll j=0; j<4; j++)
        {
            add(i,val[j],a[i].z,-a[i].tmp[j]);
            add(val[j],i+n,b[i].z,-b[i].tmp[j]);
        }
    }
    ekspfa();
    cout<<-ans<<endl;
    return 0;
}

```