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


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

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

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

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

    writeln(ans);
}


alias Edge = Tuple!(int, "to", int, "cap", int, "rev");
immutable inf = 10^^9 + 7;

int FordFulkerson(Edge[][] adj, int s, int t) {
    int n = adj.length.to!int;
    auto visited = new bool[](n);

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

        visited[u] = 1;

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

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

        return 0;
    }

    int F;

    while (true) {
        visited[] = 0;
        int f = dfs(0, inf);
        if (!f) 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);
        }
    }
}
