int dfs(int x,int flow){
if(x==T) return flow;
int tmp=res=0;
for(int i=last[x];i;i=next[i]) if (d[x]==d[v[i]]+1 && w[i]>0){
last[x]=i;
tmp=dfs(v[i],min(flow-res,w[i]);
res+=tmp;w[i]-=tmp;w[i^1]+=tmp;
if (res==flow) return flow;
}
if (d[1]>=n) return res;
vd[d[x]]--; if (vd[d[x]]==0) d[1]=n;
d[x]++;vd[d[x]]++;
last[x]=top[x];
return res;
}
int SAP{
memset(d,0,sizeof(d));
memset(vd,0,sizeof(vd);
for (int i=1;i<=n;i++) last[i]=top[i];
int ans=0;
while (d[1]<n) ans+=dfs(S,1e9);
return ans;
}