import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
import scala.io.StdIn._

object Main extends App {
  val Array(v, e) = readLine.trim.split(' ').map(_.toInt)
  val nodes = Array.fill(v){ArrayBuffer[Edge]()}
  for (_ ← 0 until e) {
    val Array(u, v, c) = readLine.trim.split(' ').map(_.toInt)
    nodes(u).append(new Edge(v, c))
    nodes(v).append(new Edge(u, 0))
    nodes(u).last.pair = nodes(v).last
    nodes(v).last.pair = nodes(u).last
  }
  val depth = Array.fill(v){0}
  val searching = Array.fill(v){0}
  var flow = 0
  var hasFlow = true
  while(hasFlow) {
    hasFlow = false
    bfs()
    var f = dfs(0)
    while(f != -1) {
      hasFlow = true
      flow += f
      f = dfs(0)
    }
  }
  println(flow)
  def bfs(): Unit = {
    val queue = mutable.Queue(0)
    depth.indices.foreach(i ⇒ depth(i) = -1)
    searching.indices.foreach(i ⇒ searching(i) = 0)
    depth(0) = 0
    while (queue.nonEmpty) {
      val current = queue.dequeue()
      for (Edge(to, flow) ← nodes(current) if flow > 0 && depth(to) == -1) {
        depth(to) = depth(current) + 1
        queue.enqueue(to)
      }
    }
  }
  def dfs(current: Int, min: Int = Int.MaxValue): Int = {
    if (current == v - 1) min
    else {
      var result = -1
      while (result == -1 && searching(current) < nodes(current).length) {
        val Edge(to, flow) = nodes(current)(searching(current))
        if (flow > 0 && depth(current) < depth(to)) {
          result = dfs(to, math.min(flow, min))
        }
        if (result == -1) {
          searching(current) += 1
        }
      }
      if (result != -1) {
        nodes(current)(searching(current)).flow -= result
        nodes(current)(searching(current)).pair.flow += result
      }
      result
    }
  }
  case class Edge(to: Int, var flow: Int) {
    private var mPair: Option[Edge] = None
    def pair: Edge = mPair.get
    def pair_=(edge: Edge): Unit = mPair = Some(edge)
  }
}

