using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Competitive
{
    internal class Solution
    {
        public int V, E;
        public Graph G;
        public void Run()
        {
            {
                var line = Input.ReadIntArray();
                V = line[0];
                E = line[1];
            }

            G = new Graph(V);
            for (int i = 0; i < E; i++)
            {
                var line = Input.ReadIntArray();
                int fr = line[0];
                int to = line[1];
                int cap = line[2];
                var e1 = new Edge(fr, to, cap);
                var e2 = new Edge(to, fr, 0);
                e1.Rev = e2;
                e2.Rev = e1;
                G[fr].Edges.Add(e1);
                G[to].Edges.Add(e2);
            }

            var ff = new FordFulkerson(G);
            int ret = ff.MaxFlow(0, V - 1);
            Console.WriteLine(ret);
        }
    }

    // libs ----------
    // 最大流問題、最悪で計算量はO(F*E)
    class FordFulkerson
    {
        public bool[] Used;
        int N;
        public Graph G;
        private int INF = 1000000;

        public FordFulkerson(Graph G)
        {
            this.G = G;
            N = G.Nodes.Count;
        }

        public int MaxFlow(int s, int t)
        {
            int ret = 0;
            while (true)
            {
                Used = new bool[N];
                int flow = DFS(s, t, INF);
                if (flow == 0) return ret;
                ret += flow;
            }
        }

        public int DFS(int ss, int tt, int flow)
        {
            if (ss == tt) return flow;
            Used[ss] = true;
            foreach (var e in G[ss].Edges)
            {
                if (!Used[e.To] && e.Cap > 0)
                {
                    int f = DFS(e.To, tt, Math.Min(flow, e.Cap));
                    if (f > 0)
                    {
                        e.Cap -= f;
                        e.Rev.Cap += f;
                        return f;
                    }
                }
            }

            return 0;
        }
    }

    internal class Graph
    {
        public List<Node> Nodes;

        public Node this[int i]
        {
            set { Nodes[i] = value; }
            get { return Nodes[i]; }
        }

        public Graph(int N)
        {
            Nodes = new List<Node>();
            for (int i = 0; i < N; i++)
            {
                Nodes.Add(new Node(i));
            }
        }

    }

    internal class Node
    {
        public List<Edge> Edges;
        public int No;

        public Node(int no)
        {
            this.No = no;
            Edges = new List<Edge>();
        }
    }

    internal class Edge
    {
        public int From;
        public int To;
        public int Cap;
        public Edge Rev;

        public Edge(int fr, int to, int cap)
        {
            From = fr;
            To = to;
            Cap = cap;
        }

    }

    // common ----------

    internal static class Input
    {
        public static string ReadString()
        {
            string line = Console.ReadLine();
            return line;
        }

        public static int ReadInt()
        {
            string line = Console.ReadLine();
            return int.Parse(line);
        }

        public static int[] ReadIntArray()
        {
            string line = Console.ReadLine();
            return line.Split(' ').Select(int.Parse).ToArray();            
        }

        public static long ReadLong()
        {
            string line = Console.ReadLine();
            return long.Parse(line);
        }

        public static long[] ReadLongArray()
        {
            string line = Console.ReadLine();
            return line.Split(' ').Select(long.Parse).ToArray();
        }

        public static double[] ReadDoubleArray()
        {
            string line = Console.ReadLine();
            return line.Split(' ').Select(double.Parse).ToArray();
        }
    }
    
    internal class Program
    {
        public static void Main(string[] args)
        {
            var s = new Solution();
            s.Run();
        }
    }
}


Measured memory_bytes: 20292