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

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

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);
    }

    debug {
        //writeln(adj);
    }

    int dfs(int u, int b, bool[] visited) {
        if (u == n - 1) {
            debug {
                //writefln("%d", u);
            }
            return b;
        }

        visited[u] = true;

        //writef("%d ", u);

        foreach (ref v ; adj[u]) {
            if (v.c > 0 && !visited[v.to]) {
                int d = dfs(v.to, min(b, v.c), visited);
                if (d > 0) {
                    v.c -= d;
                    adj[v.to][v.rev].c += d;
                    return d;
                }
            }
        }

        return 0;
    }

    int ans;

    while (true) {
        auto visited = new bool[](n);
        visited[0] = true;

        int F = dfs(0, 101101, visited);

        debug {
            //writeln("F:", F);
            //writeln(adj);
        }

        if (!F) break;

        ans += F;
    }

    writeln(ans);
}



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);
        }
    }
}