/**
 * Author: Sergey Kopeliovich (Burunduk30@gmail.com)
 * Date: 2012.05.30
 * MaxTest: 53 --> 0.07 sec
 */

#include <cstdio>
#include <cstring>
#include <cassert>
#include <algorithm>
#include <iostream>

using namespace std;

#define forn(i, n) for (int i = 0; i < (int)(n); i++)

typedef long long ll;

const int maxN = 500;
const int maxE = 20002;

int n, m, e = 2, head[maxN], to[maxE], c[maxE], next[maxE];
int S, T, h[maxN];
ll ex[maxN];
int qst, qen, q[maxN];
int run, cc, used[maxN];

void addE( int a, int b, int x )
{
  to[e] = b, c[e] = x, next[e] = head[a], head[a] = e++;
}

void ReLabel()
{
  cc++, qst = qen = 0, run = 0;
  used[T] = cc, q[qen++] = T;
  while (qst < qen)
  {
    int x, v = q[qst++];
    if (v != T && ex[v] > 0)
      run = 1;
    for (int e = head[v]; e; e = next[e])
      if (used[x = to[e]] != cc && x != S && 0 < c[e ^ 1])
        h[x] = h[v] + 1, used[x] = cc, q[qen++] = x;
  }
  forn(i, n)
    if (used[i] != cc)
      h[i] = n;
} 

int main()
{
  #define NAME "flow2"
  assert(freopen(NAME ".in", "r", stdin));
  assert(freopen(NAME ".out", "w", stdout));

  scanf("%d%d", &n, &m);
  forn(i, m)
  {
    int a, b, x;
    scanf("%d%d%d", &a, &b, &x);
    a--, b--;
    addE(a, b, x);
    addE(b, a, 0);
  }

  S = 0, T = n - 1;
  for (int e = head[S]; e; e = next[e])
    ex[to[e]] += c[e], c[e ^ 1] += c[e], c[e] = 0;
  ReLabel();
  while (run)
  {
    while (qen--)
    {
      int u, v = q[qen];
      for (int e = head[v]; e && ex[v]; e = next[e])
        if (c[e] > 0 && h[u = to[e]] + 1 == h[v])
        {
          int x = min((ll)c[e], ex[v]);
          c[e] -= x, c[e ^ 1] += x;
          ex[u] += x, ex[v] -= x;
        }
    }
    ReLabel();
  }
  cout << ex[T] << endl;
  return 0;
}