def main():

    nvertices, nedges = list(map(int, input().split()))

    adj = [[0] * nvertices for i in range(nvertices)]

    for i in range(nedges):

        u, v, c = list(map(int, input().split()))

        adj[u][v] = c



    ans = 0

    while True:

        parents = bfs(0, nvertices - 1, adj)

        if parents is None:

            break

        v = nvertices - 1

        flow = float('inf')

        while parents[v] != -1:

            u = parents[v]

            flow = min(flow, adj[u][v])

            v = u

        ans += flow

        v = nvertices - 1

        while parents[v] != -1:

            u = parents[v]

            adj[u][v] -= flow

            adj[v][u] += flow

            v = u



    print(ans)





def bfs(s, t, adj):

    nvertices = len(adj)

    visited = [False] * nvertices

    parents = [-1] * nvertices

    visited[s] = True

    que = [s]

    while que:

        u = que.pop(0)

        if u == t:

            return parents

        for v in range(nvertices):

            if not visited[v] and adj[u][v] > 0:

                visited[v] = True

                parents[v] = u

                que.append(v)



    return None





main()
def main():

    nvertices, nedges = list(map(int, input().split()))

    adj = [[0] * nvertices for j in range(nvertices)]

    for i in range(nedges):

        u, v, c = list(map(int, input().split()))

        adj[u][v] = c



    INF = float('inf')

    ans = 0

    while True:

        parents = bfs(0, nvertices - 1, adj)

        if parents is None:

            break

        v = nvertices - 1

        f = INF

        while parents[v] != -1:

            u = parents[v]

            f = min(f, adj[u][v])

            v = u



        ans += f

        v = nvertices - 1

        while parents[v] != -1:

            u = parents[v]

            adj[u][v] -= f

            adj[v][u] += f

            v = u



    print(ans)





def bfs(s, t, adj):

    nvertices = len(adj)

    visited = [False] * nvertices

    visited[s] = True

    que = [s]

    parents = [-1] * nvertices

    while que:

        u = que.pop(0)

        if u == t:

            return parents



        for v in range(nvertices):

            if not visited[v] and adj[u][v] > 0:

                parents[v] = u

                visited[v] = True

                que.append(v)



    return None





main()
def main():
    nvertices, nedges = list(map(int, input().split()))
    adj = [[0] * nvertices for i in range(nvertices)]
    for i in range(nedges):
        u, v, c = list(map(int, input().split()))
        adj[u][v] = c
    ans = 0
    while True:
        parents = bfs(0, nvertices - 1, adj)
        if parents is None:
            break
        v = nvertices - 1
        flow = float("inf")
        while parents[v] != -1:
            u = parents[v]
            flow = min(flow, adj[u][v])
            v = u
        ans += flow
        v = nvertices - 1
        while parents[v] != -1:
            u = parents[v]
            adj[u][v] -= flow
            adj[v][u] += flow
            v = u
    print(ans)


def bfs(s, t, adj):
    nvertices = len(adj)
    visited = [False] * nvertices
    parents = [-1] * nvertices
    visited[s] = True
    que = [s]
    while que:
        u = que.pop(0)
        if u == t:
            return parents
        for v in range(nvertices):
            if not visited[v] and adj[u][v] > 0:
                visited[v] = True
                parents[v] = u
                que.append(v)
    return None


main()

def main():
    nvertices, nedges = list(map(int, input().split()))
    adj = [[0] * nvertices for j in range(nvertices)]
    for i in range(nedges):
        u, v, c = list(map(int, input().split()))
        adj[u][v] = c
    INF = float("inf")
    ans = 0
    while True:
        parents = bfs(0, nvertices - 1, adj)
        if parents is None:
            break
        v = nvertices - 1
        f = INF
        while parents[v] != -1:
            u = parents[v]
            f = min(f, adj[u][v])
            v = u
        ans += f
        v = nvertices - 1
        while parents[v] != -1:
            u = parents[v]
            adj[u][v] -= f
            adj[v][u] += f
            v = u
    print(ans)


def bfs(s, t, adj):
    nvertices = len(adj)
    visited = [False] * nvertices
    visited[s] = True
    que = [s]
    parents = [-1] * nvertices
    while que:
        u = que.pop(0)
        if u == t:
            return parents
        for v in range(nvertices):
            if not visited[v] and adj[u][v] > 0:
                parents[v] = u
                visited[v] = True
                que.append(v)
    return None


main()

-    adj = [[0] * nvertices for i in range(nvertices)] +    adj = [[0] * nvertices for j in range(nvertices)] +    INF = float("inf") -        flow = float("inf") +        f = INF -            flow = min(flow, adj[u][v]) +            f = min(f, adj[u][v]) -        ans += flow +        ans += f -            adj[u][v] -= flow -            adj[v][u] += flow +            adj[u][v] -= f +            adj[v][u] += f -    parents = [-1] * nvertices +    parents = [-1] * nvertices +                parents[v] = u -                parents[v] = u