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

immutable inf = 10L^^16;
alias Edge = Tuple!(int, "from", int, "to", long, "cap", int, "rev");

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

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

    void addEdge(int u, int v, long cap) {
        adj[u] ~= Edge(u, v, cap, adj[v].length.to!int);
        adj[v] ~= Edge(v, u, 0, adj[u].length.to!int - 1);
    }

    foreach (i ; 0 .. m) {
        int u, v, c;
        scan(u, v, c);
        addEdge(u, v, c);
    }

    long ans = EdmondsKarp(adj, 0, n - 1);

    writeln(ans);
}



long EdmondsKarp(Edge[][] adj, int s, int t) {
    int n = adj.length.to!int;
    long F;

    while (true) {
        auto q = Queue!(int)(n);
        q.push(s);

        auto pred = new Edge*[](n);

        while (!q.empty && !pred[t]) {
            auto cur = q.front;
            q.pop();

            foreach (ref e ; adj[cur]) {
                if (!pred[e.to] && e.to != s && e.cap > 0) {
                    pred[e.to] = &e;
                    q.push(e.to);
                }
            }
        }

        if (pred[t]) {
            long df = inf;

            for (auto e = pred[t]; e; e = pred[e.from]) {
                df = min(df, e.cap);
            }

            for (auto e = pred[t]; e; e = pred[e.from]) {
                e.cap -= df;
                adj[e.to][e.rev].cap += df;
            }

            F += df;
        }
        else break;
    }

    return F;
}



struct Queue(T) {
    private {
        int N, head, tail;
        T[] data;
    }

    this (int n) {
        N = n + 1;
        data = new T[](N);
    }

    bool empty() {
        return head == tail;
    }

    bool full() {
        return (tail + 1) % N == head;
    }

    T front() {
        return data[head];
    }

    void push(T x) {
        assert(!full);
        data[tail++] = x;
        tail %= N;
    }

    void pop() {
        assert(!empty);
        head++;
        head %= N;
    }
}

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