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;
const long long int M = 1e9 + 7;
long long int inf = 1e18;
long long int begtime = clock();
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
long long int n, m, k;
long long int binpow(long long int val, long long int deg) {
  if (deg < 0) return 0;
  if (!deg) return 1 % M;
  if (deg & 1) return binpow(val, deg - 1) * val % M;
  long long int res = binpow(val, deg >> 1);
  return (res * res) % M;
}
long long int modinv(long long int n) { return binpow(n, M - 2); }
long long int gcd(long long int a, long long int b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
struct FlowEdge {
  long long int vector, u;
  long long cap, flow = 0;
  FlowEdge(long long int vector, long long int u, long long cap)
      : vector(vector), u(u), cap(cap) {}
};
struct Dinic {
  const long long flow_inf = 1e18;
  vector<FlowEdge> edges;
  vector<vector<long long int>> adj;
  long long int n, m = 0;
  long long int s, t;
  vector<long long int> level, ptr;
  queue<long long int> q;
  Dinic(long long int n, long long int s, long long int t) : n(n), s(s), t(t) {
    adj.resize(n);
    level.resize(n);
    ptr.resize(n);
  }
  void add_edge(long long int vector, long long int u, long long cap) {
    edges.emplace_back(vector, u, cap);
    edges.emplace_back(u, vector, 0);
    adj[vector].push_back(m);
    adj[u].push_back(m + 1);
    m += 2;
  }
  bool bfs() {
    while (!q.empty()) {
      long long int vector = q.front();
      q.pop();
      for (long long int id : adj[vector]) {
        if (edges[id].cap - edges[id].flow < 1) continue;
        if (level[edges[id].u] != -1) continue;
        level[edges[id].u] = level[vector] + 1;
        q.push(edges[id].u);
      }
    }
    return level[t] != -1;
  }
  long long dfs(long long int vector, long long pushed) {
    if (pushed == 0) return 0;
    if (vector == t) return pushed;
    for (long long int& cid = ptr[vector];
         cid < (long long int)adj[vector].size(); cid++) {
      long long int id = adj[vector][cid];
      long long int u = edges[id].u;
      if (level[vector] + 1 != level[u] || edges[id].cap - edges[id].flow < 1)
        continue;
      long long tr = dfs(u, min(pushed, edges[id].cap - edges[id].flow));
      if (tr == 0) continue;
      edges[id].flow += tr;
      edges[id ^ 1].flow -= tr;
      return tr;
    }
    return 0;
  }
  long long flow() {
    long long f = 0;
    while (true) {
      fill(level.begin(), level.end(), -1);
      level[s] = 0;
      q.push(s);
      if (!bfs()) break;
      fill(ptr.begin(), ptr.end(), 0);
      while (long long pushed = dfs(s, flow_inf)) {
        f += pushed;
      }
    }
    return f;
  }
};
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long int i, j, t, x, y, z;
  cin >> n >> m;
  vector<pair<long long int, pair<long long int, long long int>>> road;
  for (i = 0; i < m; i++) {
    cin >> x >> y >> z;
    road.push_back({z, {x, y}});
  }
  sort(road.begin(), road.end());
  long long int l = 1, r = m, mid, ans = -1;
  while (l <= r) {
    mid = (l + r) / 2;
    Dinic f(2 * n + 2, 0, 2 * n + 1);
    for (i = 0; i < mid; i++) {
      f.add_edge(road[i].second.first, road[i].second.second + n, 1);
    }
    for (i = 0; i < n; i++) {
      f.add_edge(0, i + 1, 1);
      f.add_edge(i + 1 + n, 2 * n + 1, 1);
    }
    x = f.flow();
    if (x < n)
      l = mid + 1;
    else {
      ans = road[mid - 1].first;
      r = mid - 1;
    }
  }
  cout << ans;
  return 0;
}