from collections import defaultdict



def dfs(source, sink, flow, connect):

    if source == sink:

        return flow

    used[source] = 1

    for i, (target, cost, rev_i) in enumerate(connect[source]):

        if not used[target] and cost > 0:

            that_flow = dfs(target, sink, min(flow, cost), connect)

            if that_flow > 0:

                connect[source][i][1] -= that_flow

                connect[target][rev_i][1] += that_flow

                return that_flow

    return 0



v_num, e_num = (int(n) for n in input().split(" "))

connect = defaultdict(list)

for _ in range(e_num):

    s, t, cost = (int(n) for n in input().split(" "))

    connect[s].append([t, cost, len(connect[t])])

    connect[t].append([s, 0, len(connect[s]) - 1])



answer = 0

base_used = [0 for n in range(v_num)]

while True:

    used = base_used.copy()

    now_flow = dfs(0, v_num - 1, float("inf"), connect)

    if now_flow == 0:

        break

    answer += now_flow

print(answer)
from collections import defaultdict



def bfs(start, end, connect, v_num, flow):

    level = [-1] * v_num

    level[start] = 0

    queue = [start]

    while queue:

        v = queue.pop()

        for next_v in connect[v]:

            if flow[v][next_v] > 0 and level[next_v] < 0:

                level[next_v] = level[v] + 1

                queue.append(next_v)

    return level



def dfs(here, end, connect, level, flow, minimum_f, visited):

    if here == end:

        return minimum_f

    for next_v in connect[here]:

        if not visited[here][next_v] and flow[here][next_v] > 0 and level[here] < level[next_v]:

            visited[here][next_v] = 1

            temp_f = dfs(next_v, end, connect, level, flow, min(flow[here][next_v], minimum_f), visited)

            if temp_f:

                flow[here][next_v] -= temp_f

                flow[next_v][here] += temp_f

                # print(flow)

                return temp_f

    return 0



v_num, e_num = (int(n) for n in input().split(" "))

connect = defaultdict(list)

flow = defaultdict(lambda: defaultdict(int))

for _ in range(e_num):

    s, t, c = list(map(int, input().split(" ")))

    connect[s].append(t)

    connect[t].append(s)

    flow[s][t] = c

start = 0

end = v_num - 1

answer = 0

while True:

    level = bfs(start, end, connect, v_num, flow)

    if level[end] < 0:

        print(answer)

        break

    visited = defaultdict(lambda: defaultdict(int))

    temp_f = dfs(start, end, connect, level, flow, float("inf"), visited)

    while temp_f > 0:

        answer += temp_f

        temp_f = dfs(start, end, connect, level, flow, float("inf"), visited)



from collections import defaultdict


def dfs(source, sink, flow, connect):
    if source == sink:
        return flow
    used[source] = 1
    for i, (target, cost, rev_i) in enumerate(connect[source]):
        if not used[target] and cost > 0:
            that_flow = dfs(target, sink, min(flow, cost), connect)
            if that_flow > 0:
                connect[source][i][1] -= that_flow
                connect[target][rev_i][1] += that_flow
                return that_flow
    return 0


v_num, e_num = (int(n) for n in input().split(" "))
connect = defaultdict(list)
for _ in range(e_num):
    s, t, cost = (int(n) for n in input().split(" "))
    connect[s].append([t, cost, len(connect[t])])
    connect[t].append([s, 0, len(connect[s]) - 1])
answer = 0
base_used = [0 for n in range(v_num)]
while True:
    used = base_used.copy()
    now_flow = dfs(0, v_num - 1, float("inf"), connect)
    if now_flow == 0:
        break
    answer += now_flow
print(answer)

from collections import defaultdict


def bfs(start, end, connect, v_num, flow):
    level = [-1] * v_num
    level[start] = 0
    queue = [start]
    while queue:
        v = queue.pop()
        for next_v in connect[v]:
            if flow[v][next_v] > 0 and level[next_v] < 0:
                level[next_v] = level[v] + 1
                queue.append(next_v)
    return level


def dfs(here, end, connect, level, flow, minimum_f, visited):
    if here == end:
        return minimum_f
    for next_v in connect[here]:
        if (
            not visited[here][next_v]
            and flow[here][next_v] > 0
            and level[here] < level[next_v]
        ):
            visited[here][next_v] = 1
            temp_f = dfs(
                next_v,
                end,
                connect,
                level,
                flow,
                min(flow[here][next_v], minimum_f),
                visited,
            )
            if temp_f:
                flow[here][next_v] -= temp_f
                flow[next_v][here] += temp_f
                # print(flow)
                return temp_f
    return 0


v_num, e_num = (int(n) for n in input().split(" "))
connect = defaultdict(list)
flow = defaultdict(lambda: defaultdict(int))
for _ in range(e_num):
    s, t, c = list(map(int, input().split(" ")))
    connect[s].append(t)
    connect[t].append(s)
    flow[s][t] = c
start = 0
end = v_num - 1
answer = 0
while True:
    level = bfs(start, end, connect, v_num, flow)
    if level[end] < 0:
        print(answer)
        break
    visited = defaultdict(lambda: defaultdict(int))
    temp_f = dfs(start, end, connect, level, flow, float("inf"), visited)
    while temp_f > 0:
        answer += temp_f
        temp_f = dfs(start, end, connect, level, flow, float("inf"), visited)

-def dfs(source, sink, flow, connect): -    if source == sink: -        return flow -    used[source] = 1 -    for i, (target, cost, rev_i) in enumerate(connect[source]): -        if not used[target] and cost > 0: -            that_flow = dfs(target, sink, min(flow, cost), connect) -            if that_flow > 0: -                connect[source][i][1] -= that_flow -                connect[target][rev_i][1] += that_flow -                return that_flow +def bfs(start, end, connect, v_num, flow): +    level = [-1] * v_num +    level[start] = 0 +    queue = [start] +    while queue: +        v = queue.pop() +        for next_v in connect[v]: +            if flow[v][next_v] > 0 and level[next_v] < 0: +                level[next_v] = level[v] + 1 +                queue.append(next_v) +    return level + + +def dfs(here, end, connect, level, flow, minimum_f, visited): +    if here == end: +        return minimum_f +    for next_v in connect[here]: +        if ( +            not visited[here][next_v] +            and flow[here][next_v] > 0 +            and level[here] < level[next_v] +        ): +            visited[here][next_v] = 1 +            temp_f = dfs( +                next_v, +                end, +                connect, +                level, +                flow, +                min(flow[here][next_v], minimum_f), +                visited, +            ) +            if temp_f: +                flow[here][next_v] -= temp_f +                flow[next_v][here] += temp_f +                # print(flow) +                return temp_f +flow = defaultdict(lambda: defaultdict(int)) -    s, t, cost = (int(n) for n in input().split(" ")) -    connect[s].append([t, cost, len(connect[t])]) -    connect[t].append([s, 0, len(connect[s]) - 1]) +    s, t, c = list(map(int, input().split(" "))) +    connect[s].append(t) +    connect[t].append(s) +    flow[s][t] = c +start = 0 +end = v_num - 1 -base_used = [0 for n in range(v_num)] -    used = base_used.copy() -    now_flow = dfs(0, v_num - 1, float("inf"), connect) -    if now_flow == 0: +    level = bfs(start, end, connect, v_num, flow) +    if level[end] < 0: +        print(answer) -    answer += now_flow -print(answer) +    visited = defaultdict(lambda: defaultdict(int)) +    temp_f = dfs(start, end, connect, level, flow, float("inf"), visited) +    while temp_f > 0: +        answer += temp_f +        temp_f = dfs(start, end, connect, level, flow, float("inf"), visited)