import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;

immutable inf = 10^^9 + 7;

alias Edge = Tuple!(int, "to", int, "cost", int, "rev");

void main() {
    int n, m;
    scan(n, m);

    auto adj = new Edge[][](n, 0);

    foreach (i ; 0 .. m) {
        int u, v, c;
        scan(u, v, c);
        adj[u] ~= Edge(v, c, adj[v].length.to!int);
        adj[v] ~= Edge(u, 0, adj[u].length.to!int - 1);
    }

    int ans = FordFulkerson(n, m, adj, 0, n - 1);

    writeln(ans);
}

int FordFulkerson(int n, int m, Edge[][] adj, int s, int t) {
    bool[] visited = new bool[](n);

    int dfs (int u, int f) {
        if (u == t) {
            return f;
        }

        visited[u] = 1;

        foreach (ref e ; adj[u]) {
            if (e.cost > 0 && !visited[e.to]) {
                int d = dfs(e.to, min(f, e.cost));

                if (d > 0) {
                    e.cost -= d;
                    adj[e.to][e.rev].cost += d;
                    return d;
                }
            }
        }

        return 0;
    }

    int F;

    while (true) {
        visited[] = 0;

        int f = dfs(s, inf);

        if (f == 0) break;

        F += f;
    }

    return F;
}

void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}



void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}