#include<stdio.h>
#include<queue>
using namespace std;
int x[32768],y[32768];
long long w[8][32768];
int tp[32768];
struct node{
	int wz;
	long long w;
	bool operator<(node b) const
	{
		return w<b.w;
	}
};
priority_queue<node> q[8][8];
int s=0;
void change(int wz)
{
	node u;
	u.wz=wz;
	if(wz<s)
	{
		for(int i=0;i<5;i++)
		{
			if(i==tp[wz])
			{
				continue;
			}
			u.w=w[i][wz]-w[tp[wz]][wz];
			q[tp[wz]][i].push(u);
		}
	}
	else
	{
		for(int i=1;i<=5;i++)
		{
			if(i==tp[wz])
			{
				continue;
			}
			u.w=w[i][wz]-w[tp[wz]][wz];
			q[i][tp[wz]].push(u);
		}
	}
}
queue<int> qq;
long long d[8];
bool vis[8];
int pre[8],preid[8];
void spfa()
{
	for(int i=0;i<=5;i++)
	{
		d[i]=-1000000000000000LL;
	}
	d[0]=0;
	vis[0]=true;
	qq.push(0);
	while(!qq.empty())
	{
		int u=qq.front();
		qq.pop();
		vis[u]=false;
		for(int i=0;i<=5;i++)
		{
			if(q[u][i].empty())
			{
				continue;
			}
			node __=q[u][i].top();
			if(d[u]+__.w>d[i])
			{
				d[i]=d[u]+__.w;
				pre[i]=u;
				preid[i]=__.wz;
				if(!vis[i])
				{
					vis[i]=true;
					qq.push(i);
				}
			}
		}
	}
}
int main()
{
	int n;
	scanf("%d",&n);
	n<<=1;
	while(n--)
	{
		int xx,yy,cc;
		scanf("%d%d%d",&xx,&yy,&cc);
		while(cc--)
		{
			x[s]=xx;
			y[s]=yy;
			s++;
		}
	}
	s>>=1;
	for(int i=s;i<s+s;i++)
	{
		x[i]=-x[i];
		y[i]=-y[i];
	}
	for(int i=0;i<s+s;i++)
	{
		w[1][i]=x[i]+y[i];
		w[2][i]=x[i]-y[i];
		w[3][i]=y[i]-x[i];
		w[4][i]=-x[i]-y[i];
		if(i>=s)
		{
			tp[i]=5;
		}
	}
	for(int i=0;i<s+s;i++)
	{
		change(i);
	}
	long long ans=0;
	while(1)
	{
		for(int i=0;i<=5;i++)
		{
			for(int j=0;j<=5;j++)
			{
				while(!q[i][j].empty())
				{
					node u=q[i][j].top();
					if((u.wz<s&&tp[u.wz]==i)||(u.wz>=s&&tp[u.wz]==j))
					{
						break;
					}
					q[i][j].pop();
				}
			}
		}
		spfa();
		if(d[5]<0)
		{
			break;
		}
		ans+=d[5];
		for(int i=5;i!=0;i=pre[i])
		{
			if(preid[i]>=s)
			{
				tp[preid[i]]=pre[i];
			}
			else
			{
				tp[preid[i]]=i;
			}
			change(preid[i]);
		}
	}
	printf("%lld\n",ans);
	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<stdio.h>
#include<queue>
using namespace std;
int x[32768],y[32768];
long long w[8][32768];
int tp[32768];
struct node{
	int wz;
	long long w;
	bool operator<(node b) const
	{
		return w<b.w;
	}
};
priority_queue<node> q[8][8];
int s=0;
void change(int wz)
{
	node u;
	u.wz=wz;
	if(wz<s)
	{
		for(int i=0;i<5;i++)
		{
			if(i==tp[wz])
			{
				continue;
			}
			u.w=w[i][wz]-w[tp[wz]][wz];
			q[tp[wz]][i].push(u);
		}
	}
	else
	{
		for(int i=1;i<=5;i++)
		{
			if(i==tp[wz])
			{
				continue;
			}
			u.w=w[i][wz]-w[tp[wz]][wz];
			q[i][tp[wz]].push(u);
		}
	}
}
queue<int> qq;
long long d[8];
bool vis[8];
int pre[8],preid[8];
void spfa()
{
	for(int i=0;i<=5;i++)
	{
		d[i]=-1000000000000000LL;
	}
	d[0]=0;
	vis[0]=true;
	qq.push(0);
	while(!qq.empty())
	{
		int u=qq.front();
		qq.pop();
		vis[u]=false;
		for(int i=0;i<=5;i++)
		{
			if(q[u][i].empty())
			{
				continue;
			}
			node __=q[u][i].top();
			if(d[u]+__.w>d[i])
			{
				d[i]=d[u]+__.w;
				pre[i]=u;
				preid[i]=__.wz;
				if(!vis[i])
				{
					vis[i]=true;
					qq.push(i);
				}
			}
		}
	}
}
int main()
{
	int n;
	scanf("%d",&n);
	n<<=1;
	while(n--)
	{
		int xx,yy,cc;
		scanf("%d%d%d",&xx,&yy,&cc);
		while(cc--)
		{
			x[s]=xx;
			y[s]=yy;
			s++;
		}
	}
	s>>=1;
	for(int i=s;i<s+s;i++)
	{
		x[i]=-x[i];
		y[i]=-y[i];
	}
	for(int i=0;i<s+s;i++)
	{
		w[1][i]=x[i]+y[i];
		w[2][i]=x[i]-y[i];
		w[3][i]=y[i]-x[i];
		w[4][i]=-x[i]-y[i];
		if(i>=s)
		{
			tp[i]=5;
		}
	}
	for(int i=0;i<s+s;i++)
	{
		change(i);
	}
	long long ans=0;
	while(1)
	{
		for(int i=0;i<=5;i++)
		{
			for(int j=0;j<=5;j++)
			{
				while(!q[i][j].empty())
				{
					node u=q[i][j].top();
					if((u.wz<s&&tp[u.wz]==i)||(u.wz>=s&&tp[u.wz]==j))
					{
						break;
					}
					q[i][j].pop();
				}
			}
		}
		spfa();
		if(d[5]<0)
		{
			break;
		}
		ans+=d[5];
		for(int i=5;i!=0;i=pre[i])
		{
			if(preid[i]>=s)
			{
				tp[preid[i]]=pre[i];
			}
			else
			{
				tp[preid[i]]=i;
			}
			change(preid[i]);
		}
	}
	printf("%lld\n",ans);
	return 0;
}
```