nv, ne = gets.split.map(&:to_i)

to = []
nv.times do to << [] end

ne.times do 
    u, v, c = gets.split.map(&:to_i)
    to[u] << [v, c, to[v].size]
    to[v] << [u, 0, to[u].size-1]
end

def dfs(to, seen, u, t, f)
    return f if u == t
    seen[u] = true
    to[u].each do |e|
        v, c, rev = e
        next if seen[v] or c == 0
        d = dfs(to, seen, v, t, [f, c].min)
        next if d == 0
        e[1] -= d
        to[v][rev][1] += d
        return d
    end
    return 0
end

ans = 0
while true
    seen = Array.new(nv, false)
    f = dfs(to, seen, 0, nv-1, 10005)
    if f == 0
        puts ans
        break
    else
        ans += f
    end
end

