MAX = 1.0 / 0.0
class Node
  attr_accessor :c, :cf
  def initialize
    @c = {}
    @cf = {}
  end
end

def dfs(s, t, g)
  stack = [[s, MAX]]
  done = {}
  done[s] = true
  while stack.size > 0
    u = stack[-1][0]
    return stack if u == t
    flag = true
    g[u].cf.each{|v, c|
      next if done[v]
      next if c == 0
      done[v] = true
      stack << [v, c]
      flag = false
      break
    }
    if flag
      stack.pop
    end
  end
  return nil
end

vn, e = gets.split.map(&:to_i)
g = Array.new(vn){Node.new}
e.times{
  u, v, c = gets.split.map(&:to_i)
  g[u].c[v] = c
  g[u].cf[v] = c
}

sum = 0
loop do
  #detect path from s to t
  path = dfs(0, vn-1, g)

  if path
    #maximum flow by the path is
    max = MAX
    path.size.times{|i|
      max = path[i][1] if max > path[i][1]
    }
    sum += max

    #subtract the flow
    (path.size-1).times{|i|
      s = path[i][0]
      t = path[i+1][0]
      g[s].cf[t] -= max
      g[t].cf[s] = max
      if g[s].cf[t] == 0
        g[s].cf.delete(t)
      end
    }
  else
    #there is no path
    break
  end
end

p sum