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;
long long n, m;
vector<pair<long long, pair<long long, long long> > > E;
long long get_vertex(long long v, long long idx) {
  return E[idx].second.first == v ? E[idx].second.second : E[idx].second.first;
}
long long s, t, si;
bitset<10011> c[2][10011];
bitset<10011> f[2][10011];
long long d[2 * 10011], p[2 * 10011];
vector<long long> adj_ptr;
vector<long long> adj[2 * 10011];
bool flowable(long long u, long long v) {
  long long typ = u > n;
  if (typ) {
    if (v == t)
      return !f[1][u - n][0];
    else
      return f[1][u - n][v];
  } else {
    if (u == s)
      return !f[0][0][v];
    else
      return !f[0][u][v - n];
  }
}
void add_flow(long long u, long long v) {
  long long typ = u > n;
  if (typ) {
    if (v == t)
      f[1][u - n][0] = f[1][0][u - n] = 1;
    else
      f[1][u - n][v] = f[0][v][u - n] = 0;
  } else {
    if (u == s)
      f[0][0][v] = f[0][v][0] = 1;
    else
      f[0][u][v - n] = f[1][v - n][u] = 1;
  }
}
bool make_level_graph(long long max_w) {
  memset(d, -1, sizeof d);
  d[s] = 0;
  queue<long long> q;
  q.push(s);
  while (!q.empty()) {
    long long u = q.front();
    q.pop();
    if (u == t)
      continue;
    else if (u == s) {
      for (long long i = 1; i <= n; i++) {
        if (d[i] == -1 and flowable(s, i)) {
          q.push(i);
          d[i] = 1;
        }
      }
    } else {
      for (long long idx : adj[u]) {
        long long v = get_vertex(u, idx);
        if (idx <= max_w and d[v] == -1 and flowable(u, v)) {
          q.push(v);
          d[v] = d[u] + 1;
        }
      }
      if (u > n and u != t) {
        if (d[t] == -1 and flowable(u, t)) {
          q.push(t);
          d[t] = d[u] + 1;
        }
      }
    }
  }
  return d[t] != -1;
}
bool dfs(long long u, long long max_w) {
  if (u == t) return true;
  if (u == s) {
    for (long long& v = si; v <= n; v++) {
      if (flowable(s, v) and dfs(v, max_w)) {
        p[v] = s;
        return true;
      }
    }
  } else {
    for (long long& i = adj_ptr[u]; i < (long long)adj[u].size(); i++) {
      long long idx = adj[u][i];
      long long v = get_vertex(u, idx);
      if (idx <= max_w and d[v] == d[u] + 1 and flowable(u, v) and
          dfs(v, max_w)) {
        p[v] = u;
        return true;
      }
    }
    if (u > n and u != t) {
      if (flowable(u, t) and dfs(t, max_w)) {
        p[t] = u;
        return true;
      }
    }
  }
  d[u] = -1;
  return false;
}
bool find_aug_path(long long max_w) {
  memset(p, -1, sizeof p);
  p[s] = s;
  return dfs(s, max_w);
}
long long max_flow(long long max_w) {
  for (long long i = 0; i <= n; i++) {
    f[0][i].reset();
    f[1][i].reset();
  }
  long long res = 0;
  while (make_level_graph(max_w)) {
    adj_ptr.assign(2 * n + 2, 0);
    si = 1;
    while (find_aug_path(max_w)) {
      for (long long v = t, u = p[v]; v != s; v = u, u = p[v]) add_flow(u, v);
      res++;
    }
  }
  return res;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (long long i = 0; i < m; i++) {
    long long u, v, cost;
    cin >> u >> v >> cost;
    E.emplace_back(cost, pair<long long, long long>(u, v + n));
  }
  sort(E.begin(), E.end());
  t = 2 * n + 1;
  for (long long i = 0; i < m; i++) {
    long long u = E[i].second.first;
    long long v = E[i].second.second;
    adj[u].push_back(i);
    adj[v].push_back(i);
    c[0][u][v - n] = 1;
  }
  for (long long i = 1; i <= n; i++) {
    c[0][s][i] = 1;
    c[1][i][0] = 1;
  }
  long long ans = -1;
  long long lq = 0, rq = m - 1;
  while (lq <= rq) {
    long long mid = (lq + rq) >> 1;
    if (max_flow(mid) == n) {
      ans = mid;
      rq = mid - 1;
    } else
      lq = mid + 1;
  }
  if (ans == -1)
    cout << "-1\n";
  else
    cout << E[ans].first << '\n';
  return 0;
}