#include <stdio.h>
#define ll long long
#define rep(i,l,r)for(ll i=(l);i<(r);i++)
#define INF ((1LL<<62)-(1LL<<31))
#define min(p,q)((p)<(q)?(p):(q))

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ll long long
#define rep(i,l,r)for(ll i=(l);i<(r);i++)
#define repp(i,l,r,k)for(ll i=(l);i<(r);i+=(k))
#define INF ((1LL<<62)-(1LL<<31))
#define max(p,q)((p)>(q)?(p):(q))
#define min(p,q)((p)<(q)?(p):(q))
#define bit(n,m)(((n)>>(m))&1)
int upll(const void*a, const void*b){return*(ll*)a<*(ll*)b?-1:*(ll*)a>*(ll*)b?1:0;}
int downll(const void*a, const void*b){return*(ll*)a<*(ll*)b?1:*(ll*)a>*(ll*)b?-1:0;}
void sortup(ll*a,int n){qsort(a,n,sizeof(ll),upll);}
void sortdown(ll*a,int n){qsort(a,n,sizeof(ll),downll);}
ll pom(ll a,ll n,int m){ll x=1;for(a%=m;n;n/=2)n&1?x=x*a%m:0,a=a*a%m;return x;}
//#define MOD 998244353
#define MOD 1000000007
#define invp(a,p)pom(a,p-2,p)


//sからtへの最大流 O(E flow)を横着してO(VV flow)
//辺のコストはcapa
#define VMAX 110
int vcnt;
int foodoflag[VMAX];
int resflow[VMAX][VMAX];

int foododfs(int s,int t){
	if(s==t)return 1;
	foodoflag[s]=1;
	rep(i,0,vcnt)if(!foodoflag[i]&&resflow[s][i]>0){
		if(foododfs(i,t)){
			resflow[s][i]--;
			resflow[i][s]++;
			return 1;
		}
	}
	return 0;
}
int foodo(int s,int t){
	int ans=0;
	while(foododfs(s,t)){
		ans++;
		rep(i,0,vcnt)foodoflag[i]=0;
	}
	return ans;
}
#undef VMAX

int main(){
    ll ecnt;
    scanf("%lld%lld",&vcnt,&ecnt);
    rep(i,0,ecnt){
        ll a,b,c;
        scanf("%lld%lld%lld",&a,&b,&c);
        resflow[a][b]=c;
    }
	//maxflowは(max c)|E|/2≒5e6なのでTLEするはず
    printf("%lld\n",foodo(0,vcnt-1));
}
