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
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
const long long MX = 4e4 + 5;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
const long double PI = acos((long double)-1);
const long long xd[4] = {1, 0, -1, 0}, yd[4] = {0, 1, 0, -1};
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
template <class T>
bool ckmin(T& a, const T& b) {
  return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
  return a < b ? a = b, 1 : 0;
}
constexpr long long pct(long long x) { return __builtin_popcount(x); }
constexpr long long bits(long long x) { return 31 - __builtin_clz(x); }
template <class T>
T cdiv(T a, T b) {
  return a / b + !(a < 0 || a % b == 0);
}
long long fstTrue(function<bool(long long)> first, long long lo, long long hi) {
  hi++;
  assert(lo <= hi);
  while (lo < hi) {
    long long mid = (lo + hi) / 2;
    first(mid) ? hi = mid : lo = mid + 1;
  }
  return lo;
}
template <class T>
void remDup(vector<T>& v) {
  sort(begin(v), end(v));
  v.erase(unique(begin(v), end(v)), end(v));
}
void DBG() { cerr << "]" << endl; }
template <class H, class... T>
void DBG(H h, T... t) {
  cerr << h;
  if (sizeof...(t)) cerr << ", ";
  DBG(t...);
}
void setIn(string S) { freopen(S.c_str(), "r", stdin); }
void setOut(string S) { freopen(S.c_str(), "w", stdout); }
void setIO(string S = "") {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  if ((long long)(S).size()) {
    setIn(S + ".in"), setOut(S + ".out");
  }
}
template <long long SZ>
struct Dinic {
  long long N, second, t;
  typedef long long F;
  struct Edge {
    long long to, rev;
    F flo, cap;
  };
  vector<Edge> adj[SZ];
  void reset() {
    for (long long i = (0); i < (N); ++i)
      for (auto& e : adj[i]) e.flo = 0;
  }
  void clean() {
    for (long long i = (0); i < (N); ++i) adj[i].clear();
  }
  void ae(long long u, long long v, F cap, F rcap = 0) {
    assert(min(cap, rcap) >= 0);
    Edge a{v, (long long)(adj[v]).size(), 0, cap},
        b{u, (long long)(adj[u]).size(), 0, rcap};
    adj[u].push_back(a), adj[v].push_back(b);
  }
  long long lev[SZ];
  typename vector<Edge>::iterator cur[SZ];
  bool bfs() {
    for (long long i = (0); i < (N); ++i) lev[i] = -1, cur[i] = begin(adj[i]);
    queue<long long> q({second});
    lev[second] = 0;
    while ((long long)(q).size()) {
      long long u = q.front();
      q.pop();
      for (auto& e : adj[u])
        if (lev[e.to] < 0 && e.flo < e.cap)
          q.push(e.to), lev[e.to] = lev[u] + 1;
    }
    return lev[t] >= 0;
  }
  F dfs(long long v, F flo) {
    if (v == t) return flo;
    for (; cur[v] != end(adj[v]); cur[v]++) {
      Edge& e = *cur[v];
      if (lev[e.to] != lev[v] + 1 || e.flo == e.cap) continue;
      F df = dfs(e.to, min(flo, e.cap - e.flo));
      if (df) {
        e.flo += df;
        adj[e.to][e.rev].flo -= df;
        return df;
      }
    }
    return 0;
  }
  F maxFlow(long long _N, long long _s, long long _t) {
    N = _N, second = _s, t = _t;
    assert(second != t);
    F tot = 0;
    while (bfs())
      while (F df = dfs(second, numeric_limits<F>::max())) tot += df;
    return tot;
  }
};
vector<pair<long long, pair<long long, long long>>> ed;
Dinic<MX> BP;
int32_t main() {
  setIO();
  long long N, M;
  cin >> N >> M;
  long long nodes = N * 2 + 3;
  for (long long i = (1); i < (N + 1); ++i)
    BP.ae(0, i, 1), BP.ae(i + N, nodes - 1, 1);
  map<long long, long long> comp;
  for (long long i = (0); i < (M); ++i) {
    long long u, v, d;
    cin >> u >> v >> d;
    v += N;
    comp[d];
    ed.push_back({d, {u, v}});
    BP.ae(u, v, 1);
  }
  sort(begin(ed), end(ed));
  if (BP.maxFlow(nodes, 0, nodes - 1) != N) {
    cout << -1 << "\n";
    return 0;
  }
  auto can = [&](long long k) {
    Dinic<MX> B;
    for (long long i = (1); i < (N + 1); ++i)
      B.ae(0, i, 1), B.ae(i + N, nodes - 1, 1);
    for (long long i = (0); i < (k); ++i) {
      B.ae(ed[i].second.first, ed[i].second.second, 1);
    }
    long long flo = B.maxFlow(nodes, 0, nodes - 1);
    return flo == N;
  };
  long long lo = 0, hi = M - 1, res = -1;
  while (lo <= hi) {
    long long mid = (lo + hi) / 2;
    if (can(mid)) {
      res = mid;
      hi = mid - 1;
    } else
      lo = mid + 1;
  }
  cout << ed[res - 1].first << "\n";
  return 0;
}
