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 int MOD = (int)1e9 + 7;
const int FFTMOD = 119 << 23 | 1;
const int INF = (int)1e9 + 23111992;
const long long LINF = (long long)1e18 + 23111992;
const long double PI = acos((long double)-1);
const long double EPS = 1e-9;
inline long long gcd(long long a, long long b) {
  long long r;
  while (b) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
inline long long fpow(long long n, long long k, int p = MOD) {
  long long r = 1;
  for (; k; k >>= 1) {
    if (k & 1) r = r * n % p;
    n = n * n % p;
  }
  return r;
}
template <class T>
inline int chkmin(T& a, const T& val) {
  return val < a ? a = val, 1 : 0;
}
template <class T>
inline int chkmax(T& a, const T& val) {
  return a < val ? a = val, 1 : 0;
}
inline unsigned long long isqrt(unsigned long long k) {
  unsigned long long r = sqrt(k) + 1;
  while (r * r > k) r--;
  return r;
}
inline long long icbrt(long long k) {
  long long r = cbrt(k) + 1;
  while (r * r * r > k) r--;
  return r;
}
inline void addmod(int& a, int val, int p = MOD) {
  if ((a = (a + val)) >= p) a -= p;
}
inline void submod(int& a, int val, int p = MOD) {
  if ((a = (a - val)) < 0) a += p;
}
inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; }
inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); }
inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; }
inline int sign(long double x, long double y) { return sign(x - y); }
mt19937 mt(chrono::high_resolution_clock::now().time_since_epoch().count());
inline int mrand() { return abs((int)mt()); }
inline int mrand(int k) { return abs((int)mt()) % k; }
namespace HopcroftKarp {
const int maxv = 1e4 + 5;
const int maxe = 1e6 + 5;
int nx, ny, E, nmatches;
int adj[maxe], nxt[maxe];
int lst[maxv], ptr[maxv], lev[maxv], que[maxv], matx[maxv], maty[maxv];
void init(int _nx, int _ny) {
  nx = _nx, ny = _ny;
  E = nmatches = 0, fill_n(lst, nx, -1);
  fill_n(matx, nx, -1), fill_n(maty, ny, -1);
}
void add(int x, int y) { adj[E] = y, nxt[E] = lst[x], lst[x] = E++; }
int bfs() {
  int qh = 0, qe = 0;
  for (int x = 0; x < nx; x++) {
    if (~matx[x]) {
      lev[x] = 0;
    } else {
      lev[x] = 1;
      que[qe++] = x;
    }
  }
  int res = 0;
  while (qh < qe) {
    int x = que[qh++];
    for (int e = lst[x]; ~e; e = nxt[e]) {
      int y = adj[e];
      if (!~maty[y]) {
        res = 1;
      } else if (!lev[maty[y]]) {
        lev[maty[y]] = lev[x] + 1;
        que[qe++] = maty[y];
      }
    }
  }
  return res;
}
int dfs(int x) {
  for (int& e = ptr[x]; ~e; e = nxt[e]) {
    int y = adj[e];
    if (!~maty[y] || (lev[maty[y]] == lev[x] + 1 && dfs(maty[y]))) {
      matx[x] = y;
      maty[y] = x;
      return 1;
    }
  }
  return 0;
}
int maxmat() {
  while (bfs()) {
    for (int x = 0; x < nx; x++) {
      ptr[x] = lst[x];
    }
    for (int x = 0; x < nx; x++) {
      if (!~matx[x]) {
        nmatches += dfs(x);
      }
    }
  }
  return nmatches;
}
int vis[2][maxv];
void dfs(int u, int r) {
  vis[r][u] = 1;
  if (!r) {
    for (int e = lst[u]; ~e; e = nxt[e]) {
      int v = adj[e];
      if (matx[u] != v && !vis[!r][v]) {
        dfs(v, !r);
      }
    }
  } else {
    int v = maty[u];
    if (~v && !vis[!r][v]) {
      dfs(v, !r);
    }
  }
}
vector<int> mincover() {
  maxmat();
  vector<int> res;
  fill_n(vis[0], nx, 0), fill_n(vis[1], ny, 0);
  for (int x = 0; x < nx; x++) {
    if (!~matx[x] && !vis[0][x]) {
      dfs(x, 0);
    }
  }
  for (int x = 0; x < nx; x++) {
    if (!vis[0][x]) {
      res.push_back(x);
    }
  }
  for (int y = 0; y < ny; y++) {
    if (vis[1][y]) {
      res.push_back(nx + y);
    }
  }
  return res;
}
vector<int> maxind() {
  maxmat();
  vector<int> res;
  fill_n(vis[0], nx, 0), fill_n(vis[1], ny, 0);
  for (int x = 0; x < nx; x++) {
    if (!~matx[x] && !vis[0][x]) {
      dfs(x, 0);
    }
  }
  for (int x = 0; x < nx; x++) {
    if (vis[0][x]) {
      res.push_back(x);
    }
  }
  for (int y = 0; y < ny; y++) {
    if (!vis[1][y]) {
      res.push_back(nx + y);
    }
  }
  return res;
}
}  // namespace HopcroftKarp
void chemthan() {
  int n, m;
  cin >> n >> m;
  vector<tuple<int, int, int>> edges;
  for (int i = (0); i < (m); ++i) {
    int u, v, d;
    cin >> u >> v >> d;
    u--, v--;
    edges.push_back({u, v, d});
  }
  int lo = 0, hi = INF;
  while (lo < hi) {
    int mi = lo + hi >> 1;
    HopcroftKarp::init(n, n);
    for (auto& [u, v, d] : edges)
      if (d <= mi) {
        HopcroftKarp::add(u, v);
      }
    if (HopcroftKarp::maxmat() < n) {
      lo = mi + 1;
    } else {
      hi = mi;
    }
  }
  int res = (lo + hi >> 1);
  if (res == INF) res = -1;
  cout << res << "\n";
}
int32_t main(int32_t argc, char* argv[]) {
  ios_base::sync_with_stdio(0), cin.tie(0);
  if (argc > 1) {
    assert(freopen(argv[1], "r", stdin));
  }
  if (argc > 2) {
    assert(freopen(argv[2], "wb", stdout));
  }
  chemthan();
  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}