#include <bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f3f3f3f3fll
#define sc(x) scanf("%d",&x)
#define scc(x,y) scanf("%d%d",&x,&y)
#define sccc(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define file(x) freopen(#x".in","w",stdout);
using namespace std;
using std::default_random_engine;
const int mod = 998244353;
const int N = 3010;
const int M = 2e5 + 10;
namespace ISAP
{
LL H[M],d[M],cur[M],pre[M],gap[M],Q[M];
struct Edge{int u,v,c,n;} E[M];
LL nv,flow,head,tail,cntE,f;
void init(){cntE=0; memset(H,-1,sizeof(H));}
void addedge(int u,int v,int c)
{
E[cntE]=Edge{u,v,c,H[u]}; H[u]=cntE++;
E[cntE]=Edge{v,u,0,H[v]}; H[v]=cntE++;
}
void revbfs(int s,int t)
{
head=tail=0 ;
memset(d,-1,sizeof(d));
memset(gap,0,sizeof(gap));
Q[tail++]=t;d[t]=0;gap[d[t]]=1;
while (head!=tail)
{
int u=Q[head++];
for (int i=H[u];~i;i=E[i].n)
{
int v=E[i].v; if (~d[v]) continue;
d[v]=d[u]+1; gap[d[v]]++; Q[tail++]=v;
}
}
}
LL isap(int s,int t)
{
memcpy(cur,H,sizeof(cur)); nv=t;
flow=0; revbfs(s,t); int u=pre[s]=s,i;
while (d[s]<nv)
{
if (u==t)
{
f=INF;
for (i=s;i!=t;i=E[cur[i]].v)
if (f>E[cur[i]].c) f=E[cur[i]].c,u=i;
flow += f;
for (i=s;i!=t;i=E[cur[i]].v)
E[cur[i]].c-=f,E[cur[i]^1].c+=f;
}
for (i=cur[u];~i;i=E[i].n)
if (E[i].c&&d[u]==d[E[i].v]+1) break ;
if (~i) cur[u]=i,pre[E[i].v]=u,u=E[i].v;
else
{
if (0==--gap[d[u]]) break ;
LL minv=nv,v;
for (int i=H[u];~i;i=E[i].n)
{
v=E[i].v;
if (E[i].c&&minv>d[v]) minv=d[v],cur[u]=i;
}
d[u]=minv+1; gap[d[u]]++; u=pre[u];
}
}
return flow ;
}
}
ISAP::addedge(u,v,w);
ISAP::isap(s,t);