#include <bits/stdc++.h>

#ifndef LOCAL_
#define fprintf if( false ) fprintf
#endif // LOCAL_
// #define dump() fprintf(stderr, "#%s.%d\n", __func__, __LINE__);
#define dumpl(x1) fprintf(stderr, "#%s.%d (%s) = (%ld)\n", __func__, __LINE__, #x1, x1);
#define dumpll(x1, x2) fprintf(stderr, "#%s.%d (%s, %s) = (%ld, %ld)\n", __func__, __LINE__, #x1, #x2, x1, x2);
#define dumplll(x1, x2, x3) fprintf(stderr, "#%s.%d (%s, %s, %s) = (%ld, %ld, %ld)\n", __func__, __LINE__, #x1, #x2, #x3, x1, x2, x3);
#define dumpd(x1) fprintf(stderr, "#%s.%d (%s) = (%lf)\n", __func__, __LINE__, #x1, x1);
#define dumpdd(x1, x2) fprintf(stderr, "#%s.%d (%s, %s) = (%lf, %lf)\n", __func__, __LINE__, #x1, #x2, x1, x2);
#define loop for(;;)

template<typename T> void scan1(T& x) { fprintf(stderr, "unknown type\n"); }
template<> void scan1(long& x) { if( scanf("%ld", &x) < 0 ) exit(0); }
void scan() {}
template<typename Head, typename... Tail>
void scan(Head& x, Tail&... xs) {
  scan1(x); scan(xs...);
}

template<typename W>
struct N008 {
   typedef std::vector<long> LI;
   typedef std::vector<W>    LW;
   long n, e;
   LI ss, ds;
   LW ws;
   std::vector<LI> fi;
   std::vector<LI> ri;
   N008(long n_, const LI& ss_, const LI& ds_, const LW& ws_)
      : n(n_), e(ss_.size()), ss(ss_), ds(ds_), ws(ws_) {
      ri.resize(n+1);
      fi.resize(n+1);
      for(long i = 0; i < e; ++i) {
         fi[ss[i]].push_back(i);
         ri[ds[i]].push_back(i);
      }
      // for(long i = 0; i < n + 1; ++i) {
      //    fprintf(stderr, "fi[%ld] ", i);
      //    for(long r : fi[i]) {
      //       fprintf(stderr, "%ld ", r);
      //    }
      //    fprintf(stderr, "\n");
      //    fprintf(stderr, "ri");
      //    for(long r : ri[i]) {
      //       fprintf(stderr, "%ld ", r);
      //    }
      //    fprintf(stderr, "\n");
      // }
   }
};

template<typename W>
struct N009 {
   typedef std::vector<long> LI;
   typedef std::vector<W>    LW;
   const N008<W>& g;
   LW xs;
   std::vector<bool> visited;
   W res;
   N009(const N008<W>& g_, long s, long t, W inf)
      : g(g_), xs(g.e, 0), visited(g.n+1) {
      res = 0;
      for(;;) {
         initDFS();
         W a = dfs(s, t, inf);
         res += a;
         if( a == 0 ) break;
      }
   }
   void initDFS() {
      for(long i = 0; i < (long)visited.size(); ++i) visited[i] = false;
   }
   W dfs(long s, long t, W f) {
      dumplll(s, t, f);
      if( s == t ) return f;
      if( visited[s] ) return 0;
      visited[s] = true;
      for(bool b : {true, false}) {
         for(long i : b ? g.fi[s] : g.ri[s]) {
            long to = b ? g.ds[i] : g.ss[i];
            W rem = std::max((long)0, b ? g.ws[i] - xs[i] : xs[i]);
            if( rem == 0 ) continue;
            W a = dfs(to, t, std::min(f, rem));
            if( a == 0 ) continue;
            xs[i] += b ? a : -a;
            fprintf(stderr, "back : %ld\n", s);
            return a;
         }
      }
      return 0;
   }
};

struct Solver {
   Solver() { fprintf(stderr, "--------Solver begin--------\n"); }
   ~Solver() { fprintf(stderr, "--------Solver end--------\n"); }
   void solve() {
      long v, e;
      scan(v, e);
      std::vector<long> ss(e), ds(e), ws(e);
      for(long i = 0; i < e; ++i) {
         scan(ss[i], ds[i], ws[i]);
      }
      N008<long> g(v, ss, ds, ws);
      for(long i = 0; i < e; ++i) {
         dumplll(ss[i], ds[i], ws[i]);
      }
      N009<long> ford(g, 0, v - 1, (1LL << 50));
      printf("%ld\n", ford.res);
   }
};

int main() {
  loop std::unique_ptr<Solver>(new Solver())->solve();
}

Measured memory_bytes: 3336