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;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
mt19937_64 rnd64(
    chrono::high_resolution_clock::now().time_since_epoch().count());
long long power(long long a, long long m, long long mod) {
  long long ans = 1;
  while (m) {
    if (m % 2) ans *= a, ans %= (mod);
    a = (a * a) % mod;
    m >>= 1;
  }
  return ans;
}
struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
  size_t operator()(pair<int64_t, int64_t> Y) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(Y.first * 31 + Y.second + FIXED_RANDOM);
  }
};
const long long inf = 1e18;
const long long M = 1000000007;
struct hopcroft_karp {
  const long long now_inf = 1e9 + 7;
  long long n, m;
  vector<long long> ml, mr, dist;
  vector<vector<long long> > adj;
  list<long long> queue;
  hopcroft_karp(long long n, long long m) {
    this->m = m;
    this->n = n;
    dist.resize(n + 1);
    ml.resize(n + 1, 0);
    mr.resize(m + 1, 0);
    adj.resize(n + 1);
  }
  void addEdge(long long u, long long v) { adj[u].push_back(v); }
  bool bfs() {
    long long i;
    queue.clear();
    dist[0] = now_inf;
    for (i = 1; i < n + 1; i++) {
      if (!ml[i])
        queue.push_back(i), dist[i] = 0;
      else
        dist[i] = now_inf;
    }
    while (!queue.empty()) {
      long long z = queue.front();
      queue.pop_front();
      for (auto &i : adj[z])
        if (dist[mr[i]] == now_inf)
          queue.push_back(mr[i]), dist[mr[i]] = dist[z] + 1;
    }
    return dist[0] != now_inf;
  }
  bool dfs(long long u) {
    if (!u) return 1;
    for (auto &i : adj[u]) {
      if (dist[mr[i]] == dist[u] + 1 && dfs(mr[i])) {
        ml[u] = i, mr[i] = u;
        return 1;
      }
    }
    dist[u] = now_inf;
    return 0;
  }
  long long match() {
    long long ans = 0, i;
    while (bfs())
      for (i = 1; i < n + 1; i++)
        if (!ml[i] && dfs(i)) ++ans;
    return ans;
  }
};
void solve() {
  long long i, j, _;
  long long n, m;
  cin >> n >> m;
  vector<pair<long long, pair<long long, long long> > > v(m);
  for (i = 0; i < m; i++)
    cin >> v[i].second.first >> v[i].second.second >> v[i].first;
  long long l = 1, r = 1e9 + 1;
  long long ans = r;
  while (l <= r) {
    long long mid = (l + r) / 2;
    hopcroft_karp flow(n, n);
    for (i = 0; i < m; i++) {
      if (v[i].first <= mid) {
        flow.addEdge(v[i].second.first, v[i].second.second);
      }
    }
    long long now = flow.match();
    if (now < n) {
      l = mid + 1;
    } else {
      ans = min(ans, mid);
      r = mid - 1;
    }
  }
  if (ans == 1e9 + 1) ans = -1;
  cout << ans << '\n';
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long T = 1;
  while (T--) {
    solve();
  }
  return 0;
}