Problem: The pandemic is upon us, and the world is in shortage of the most important resource: toilet paper. As one of the best prepared nations for this crisis, BubbleLand promised to help all other world nations with this valuable resource. To do that, the country will send airplanes to other countries carrying toilet paper.

In BubbleLand, there are N toilet paper factories, and N airports. Because of how much it takes to build a road, and of course legal issues, every factory must send paper to only one airport, and every airport can only take toilet paper from one factory.

Also, a road can't be built between all airport-factory pairs, again because of legal issues. Every possible road has number d given, number of days it takes to build that road.

Your job is to choose N factory-airport pairs, such that if the country starts building all roads at the same time, it takes the least amount of days to complete them.

Input

The first line contains two integers N (1 ≤ N ≤ 10^4) - number of airports/factories, and M (1 ≤ M ≤ 10^5) - number of available pairs to build a road between.

On next M lines, there are three integers u, v (1 ≤ u,v ≤ N), d (1 ≤ d ≤ 10^9) - meaning that you can build a road between airport u and factory v for d days.

Output

If there are no solutions, output -1. If there exists a solution, output the minimal number of days to complete all roads, equal to maximal d among all chosen roads.

Example

Input


3 5
1 2 1
2 3 2
3 3 3
2 1 4
2 2 5


Output


4

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
void IO() {
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
const long long int INF = (long long int)1e18;
const long long int C = (long long int)1e15;
struct Dinic {
  struct Edge {
    int v, to;
    long long int cap;
    Edge() : v(), to(), cap() {}
    Edge(int _v, int _to, long long int _cap) : v(_v), to(_to), cap(_cap) {}
  };
  vector<Edge> ed;
  vector<vector<int>> g;
  int second, T;
  int n;
  vector<int> dist;
  vector<int> idx;
  Dinic() : ed(), g(), second(), T() {}
  Dinic(int _n, int _S, int _T) {
    n = _n;
    second = _S;
    T = _T;
    g.resize(n);
    for (int i = 0; i < n; i++) g[i].clear();
    ed = vector<Edge>();
  }
  void addEdge(int v, int to, long long int cap) {
    g[v].push_back((int)ed.size());
    ed.push_back(Edge(v, to, cap));
    g[to].push_back((int)ed.size());
    ed.push_back(Edge(to, v, 0));
  }
  bool BFS() {
    dist.resize(n);
    vector<int> q;
    for (int i = 0; i < n; i++) dist[i] = n;
    q.push_back(second);
    dist[second] = 0;
    for (int i = 0; i < (int)q.size(); i++) {
      int v = q[i];
      for (int id : g[v]) {
        Edge e = ed[id];
        if (e.cap <= 0) continue;
        int u = e.to;
        if (dist[u] <= dist[v] + 1) continue;
        dist[u] = dist[v] + 1;
        q.push_back(u);
      }
    }
    return dist[T] < n;
  }
  long long int dfs(int v, long long int flow) {
    if (v == T || flow == 0) return flow;
    long long int res = 0;
    for (int &i = idx[v]; i < (int)g[v].size(); i++) {
      int id = g[v][i];
      Edge e = ed[id];
      int to = e.to;
      if (dist[to] != dist[v] + 1) continue;
      long long int df = dfs(to, min(flow, e.cap));
      flow -= df;
      res += df;
      ed[id].cap -= df;
      ed[id ^ 1].cap += df;
      if (flow == 0) return res;
    }
    return res;
  }
  long long int Flow() {
    idx.resize(n);
    long long int res = 0;
    while (BFS()) {
      for (int i = 0; i < n; i++) idx[i] = 0;
      res += dfs(second, INF);
    }
    return res;
  }
} G;
long long int n, m;
vector<pair<int, pair<int, int>>> edges;
int main() {
  IO();
  cin >> n >> m;
  edges.resize(m);
  for (__typeof(m) i = (0) - ((0) > (m)); i != (m) - ((0) > (m));
       i += 1 - 2 * ((0) > (m))) {
    cin >> edges[i].second.first >> edges[i].second.second >> edges[i].first;
    edges[i].second.first--;
    edges[i].second.second--;
  }
  sort(edges.begin(), edges.end());
  int l = 0, h = m - 1;
  int second = 2 * n, T = 2 * n + 1;
  int ans = -1;
  while (l <= h) {
    int mid = l + (h - l) / 2;
    G = Dinic(2 * n + 2, second, T);
    for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n));
         i += 1 - 2 * ((0) > (n))) {
      G.addEdge(second, i, 1);
      G.addEdge(n + i, T, 1);
    }
    for (__typeof(mid + 1) i = (0) - ((0) > (mid + 1));
         i != (mid + 1) - ((0) > (mid + 1)); i += 1 - 2 * ((0) > (mid + 1))) {
      G.addEdge(edges[i].second.first, n + edges[i].second.second, 1);
    }
    if (G.Flow() == n) {
      ans = mid;
      h = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  if (ans == -1)
    cout << -1;
  else
    cout << edges[ans].first;
  return 0;
}