INF = 1<<29

def dfs(v, f)
  return f if v == $goal
  $used[v] = true
  $g[v].each do |u, c|
    if !$used[u] && c > 0
      d = dfs(u, [f, c].min)
      if d > 0
        $g[v][u] -= d
        $g[u][v] += d
        return d
      end
    end
  end
  0
end

def max_flow(size)
  flow = 0
  loop do
    $used =  [false] * size
    f = dfs($start, INF)
    return flow if f == 0
    flow += f
  end
end

v, e = gets.split.map(&:to_i)
$g = v.times.map { {} }
$start = 0
$goal = v - 1

e.times do
  s, t, c = gets.split.map(&:to_i)
  $g[s][t] = c
  $g[t][s] = 0
end

p max_flow(v)