#include <bits/stdc++.h>
 
#ifndef LOCAL_
#define fprintf if( false ) fprintf
#endif // LOCAL_
#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(;;)
 
struct S002 {
  int n;
  S002& operator > (long& x) {
    n = x;
    return *this;
  }
  S002& operator > (long&& x) {
    n = x;
    return *this;
  }
  S002& operator >= (long& x) {
    if( scanf("%ld", &x) <= 0 ) exit(0);
    return *this;
  }
  S002& operator >= (double& x) {
    if( scanf("%lf", &x) <= 0 ) exit(0);
    return *this;
  }
  S002& operator >= (std::string& s) {
    if( not (std::cin >> s) ) exit(0);
    return *this;
  }
  template<typename a>
  S002& operator >= (std::vector<a>& v) {
    v.resize(n);
    for(long i = 0; i < n; ++i) {
      *this >= v[i];
    }
    return *this;
  }
  template<typename a, std::size_t s>
  S002& operator >= (std::array<a, s>& x) {
    for(long i = 0; i < (long)s; ++i) {
      *this >= x[i];
    }
    return *this;
  }
};

template<typename a>
struct S005 {
  typedef a e;
  std::vector<std::vector<a>> xs;
  const std::vector<a>& ys;
  long n;
  S005(const std::vector<a>& ys_, long n_) : ys(ys_), n(n_) {
    xs.resize(n);
    for(a x : ys) {
      a y = x;
      xs[x.s].push_back(y);
    }
  }
  const std::vector<a>& operator [] (long i) const {
    return xs[i];
  }
  long v() const {
    return n;
  }
  const std::vector<a>& es() const {
    return ys;
  }
};

template<typename a>
struct S029 {
  typedef a w;
  S029() : s(-1), d(-1) {}
  S029(long s_, long d_, a c_) : s(s_), d(d_), c(c_) {}
  long s, d;
  a c;
};

template<typename e>
struct S030 {
  struct Edge {
    Edge(long s_, long d_, typename e::w c_, typename e::w f_, long rev_)
      : s(s_), d(d_), c(c_), f(f_), rev(rev_) {}
    long s, d;
    typename e::w c, f;
    long rev;
  };
  typename e::w operator()(const std::vector<e>& edges, long v, long src, long dest) {
    std::vector<std::vector<Edge>> graph(v);
    for(e edge : edges) {
      long r[2] = {(long)graph[edge.s].size(), (long)graph[edge.d].size()};
      graph[edge.s].push_back(Edge(edge.s, edge.d, edge.c, 0, r[1]));
      graph[edge.d].push_back(Edge(edge.d, edge.s, 0, 0, r[0]));
    }
    typename e::w res = 0;
    loop {
      long temp = flow(graph, src, dest);
      // dumpl(temp);
      if( res += temp, temp == 0 ) break;
    }
    return res;
  }
  typename e::w flow(std::vector<std::vector<Edge>>& graph, long src, long dest) {
    std::vector<Edge*> parent(graph.size(), nullptr);
    std::queue<std::pair<long, typename e::w>> q;
    q.push(std::make_pair(src, (1 << 29)));
    while( not q.empty() ) {
      std::pair<long, typename e::w> x = q.front(); q.pop();
      if( x.first == dest ) {
        update(graph, parent, dest, x.second);
        return x.second;
      }
      for(Edge& edge : graph[x.first]) {
        // fprintf(stderr, "edge : %ld -> %ld, c = %ld, f = %ld\n", edge.s, edge.d, edge.c, edge.f);
        typename e::w f = std::min(x.second, edge.c - edge.f);
        if( f <= 0 ) continue;
        if( parent[edge.d] != nullptr ) continue;
        if( edge.d == src ) continue;
        parent[edge.d] = &edge;
        // fprintf(stderr, "can flow = %ld\n", f);
        q.push(std::make_pair(edge.d, f));
      }
    }
    return 0;
  }
  void update(std::vector<std::vector<Edge>>& graph, std::vector<Edge*>& parent, long dest, typename e::w w) {
    // dumpl(dest);
    if( parent[dest] == nullptr ) return;
    parent[dest]->f += w;
    graph[parent[dest]->d][parent[dest]->rev].f -= w;
    assert( parent[dest]->s != dest );
    update(graph, parent, parent[dest]->s, w);
  }
};

struct Solver {
  typedef S029<long> Edge;
  typedef S005<Edge> Graph;
  long v, e;
  std::vector<std::array<long, 3>> xs;
  Solver() {
    S002 reader;
    reader >= v >= e >e>= xs;
  }
  void solve() {
    std::vector<Edge> edges;
    for(std::array<long, 3> x : xs) {
      edges.push_back(Edge(x[0], x[1], x[2]));
    }
    S030<Edge> maxFlow;
    long res = maxFlow(edges, v + 10, 0, v - 1);
    printf("%ld\n", res);
  }
};

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

Measured memory_bytes: 3308