#include <bits/stdc++.h>

using namespace std;

struct Edge {
  int from, to;
  Edge(int from, int to) : from(from), to(to) {};
};

template<typename Cost> struct WeightedEdge : public Edge {
  typedef Cost CostType;
  Cost cost;
  WeightedEdge(int from, int to, Cost cost) : Edge(from, to), cost(cost) {}
};

template<typename Capacity> struct ResidualEdge : public Edge {
  typedef Capacity CapacityType;
  Capacity cap;
  int rev;
  ResidualEdge(int from, int to, Capacity cap) : Edge(from, to), cap(cap) {}
  ResidualEdge reverse() const {return ResidualEdge(to, from, 0);}
};

template<typename Capacity, typename Cost> struct WeightedResidualEdge : public ResidualEdge<Capacity> {
  Cost cost;
  WeightedResidualEdge(int from, int to, Capacity cap, Cost cost) : ResidualEdge<Capacity>(from, to, cap), cost(cost) {}
  WeightedResidualEdge reverse() const {return WeightedResidualEdge(this->to, this->from, 0, -cost);}
};

template<typename Edge> class Graph {
public:
  typedef Edge EdgeType;
  virtual int size() const = 0;
  template<typename... Args> void addEdge(Args...) {}
  template<typename... Args> void addUndirectedEdge(Args...) {}
  virtual vector<Edge> getEdges(int from, int to) const = 0;
  virtual int getDegree(int v) const = 0;
};

template<typename Edge> class AdjacencyList : public Graph<Edge> {
protected:
  vector<vector<Edge>> graph;

public:
  AdjacencyList(int n) : graph(n) {}

  int size() const {
    return graph.size();
  }
  
  template<typename... Args> void addEdge(Args... args) {
    Edge edge(args...);
    graph[edge.from].emplace_back(edge);
  }

  template<typename... Args> void addUndirectedEdge(Args... args) {
    Edge edge(args...);
    addEdge(edge);
    swap(edge.from, edge.to);
    addEdge(edge);
  }

  vector<Edge> getEdges(int from, int to) const {
    vector<Edge> res;
    for (const auto& edge : graph[from]) {
      if (edge.to == to) res.emplace_back(edge);
    }
    return res;
  }

  int getDegree(int v) const {
    return graph[v].size();
  }

  vector<Edge>& operator[](int v) {
    return graph[v];
  }
};

template<typename Edge> class ResidualGraph : public AdjacencyList<Edge> {
public:
  ResidualGraph(int n) : AdjacencyList<Edge>(n) {}

  template<typename... Args> void addEdge(Args... args) {
    Edge edge(args...);
    edge.rev = this->graph[edge.to].size();
    this->graph[edge.from].emplace_back(edge);
    Edge rev = edge.reverse();
    this->graph[rev.from].emplace_back(rev);
  }

  template<typename... Args> void addUndirectedEdge(Args... args) {
    Edge edge(args...);
    edge.rev = this->graph[edge.to].size();
    this->graph[edge.from].emplace_back(edge);
    Edge rev = edge.reverse();
    rev.cap = edge.cap;
    this->graph[rev.from].emplace_back(rev);
  }

  void flow(int v, int i, typename Edge::CapacityType f) {
    auto& e = this->graph[v][i];
    e.cap -= f;
    this->graph[e.to][e.rev].cap += f;
  }
};

class MaxFlow {
private:
  template<typename Capacity> class Dinic {
  private:
    const Capacity INF;
    
    ResidualGraph<ResidualEdge<Capacity>>& graph;
    vector<int> level, iter;
    
    void bfs(int source) {
      fill(level.begin(), level.end(), -1);
      level[source] = 0;
      queue<int> que;
      que.push(source);
      while (!que.empty()) {
        int v = que.front();
        que.pop();
        for (const auto& edge : graph[v]) {
          if (edge.cap == 0 || level[edge.to] >= 0) continue;
          level[edge.to] = level[v] + 1;
          que.push(edge.to);
        }
      }
    }
    
    int dfs(int v, int sink, int flow) {
      if (v == sink) return flow;
      for (int& i = iter[v]; i < int(graph[v].size()); ++i) {
        auto& edge = graph[v][i];
        if (edge.cap == 0 || level[v] >= level[edge.to]) continue;
        int f = dfs(edge.to, sink, min(flow, edge.cap));
        if (f == 0) continue;
        graph.flow(v, i, f);
        return f;
      }
      return 0;
    }
    
  public:
    Dinic(ResidualGraph<ResidualEdge<Capacity>>& graph) : INF(numeric_limits<Capacity>::max()), graph(graph) {}
    
    Capacity solve(int source, int sink) {
      level = vector<int>(graph.size(), 0);
      iter = vector<int>(graph.size(), 0);
      int flow = 0, f;
      while (true) {
        bfs(source);
        if (level[sink] < 0) return flow;
        fill(iter.begin(), iter.end(), 0);
        while ((f = dfs(source, sink, INF)) > 0) flow += f;
      }
    }
  };

public:
  template<typename Capacity> Capacity solve(ResidualGraph<ResidualEdge<Capacity>>& graph, int source, int sink) {
    Dinic<Capacity> dinic(graph);
    return dinic.solve(source, sink);
  }
};

int main() {
  int v, e;
  cin >> v >> e;
  ResidualGraph<ResidualEdge<int>> graph(v);
  for (int i = 0; i < e; ++i) {
    int u, v, c;
    cin >> u >> v >> c;
    graph.addEdge(u, v, c);
  }
  MaxFlow maxFlow;
  cout << maxFlow.solve(graph, 0, v - 1) << endl;
}

Measured memory_bytes: 1264