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 N = 1e6 + 10;
const long long mod = 1e9 + 7;
template <typename T>
inline T gcd(T a, T b) {
  return !b ? a : gcd(b, a % b);
}
template <typename T>
inline T q_pow(T a, T x) {
  T ans = 1, tmp = a;
  while (x) {
    if (x & 1) (ans *= tmp) %= mod;
    (tmp *= tmp) %= mod;
    x >>= 1;
  }
  return ans;
}
template <typename T>
inline void re(T& N) {
  int f = 1;
  char c;
  while ((c = getchar()) < '0' || c > '9')
    if (c == '-') f = -1;
  N = c - '0';
  while ((c = getchar()) >= '0' && c <= '9') N = N * 10 + c - '0';
  N *= f;
}
int m, n, t = 1, st, en;
template <class T>
struct MF {
  struct edge {
    int v;
    T cap;
    edge() {}
    edge(int v = 0, T cap = 0) : v(v), cap(cap) {}
  };
  int m, n, st, en;
  T flow, inf = typeid(T) == typeid(int) ? 1e9 : 1e18;
  vector<edge> edges;
  vector<vector<int> > G;
  vector<int> dep, cur;
  void init(int _n) {
    flow = 0;
    G.assign(n + 1, vector<int>());
    dep.assign(n + 1, -1);
    cur.assign(n + 1, 0);
    edges.clear();
  }
  void build(int _n) {
    n = _n;
    st = n - 1;
    en = n;
    init(n);
  }
  void build(int _n, int _st, int _en) {
    n = _n;
    st = _st;
    en = _en;
    init(n);
  }
  void add(int u, int v, T cap) {
    if (u == v) return;
    edges.emplace_back(v, cap);
    edges.emplace_back(u, 0);
    m = edges.size();
    G[u].push_back(m - 2);
    G[v].push_back(m - 1);
  }
  int bfs() {
    fill(dep.begin(), dep.end(), -1);
    queue<int> q;
    q.push(st);
    dep[st] = 0;
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      for (int i = 0; i < G[u].size(); i++) {
        edge& e = edges[G[u][i]];
        if (dep[e.v] == -1 && e.cap) {
          q.push(e.v);
          dep[e.v] = dep[u] + 1;
        }
      }
    }
    return dep[en] != -1;
  }
  T dfs(int u, T lim) {
    if (u == en || !lim) return lim;
    T flow = 0;
    for (int& i = cur[u]; i < G[u].size(); i++) {
      edge& e = edges[G[u][i]];
      if (dep[e.v] != dep[u] + 1 || !e.cap) continue;
      T tmp = dfs(e.v, min(lim, e.cap));
      e.cap -= tmp;
      edges[G[u][i] ^ 1].cap += tmp;
      lim -= tmp;
      flow += tmp;
      if (!lim) break;
    }
    return flow;
  }
  T get() {
    while (bfs()) {
      fill(cur.begin(), cur.end(), 0);
      flow += dfs(st, inf);
    }
    return flow;
  }
};
struct node {
  int x, y, d;
} a[N];
MF<int> f;
int main() {
  re(n);
  re(m);
  for (int i = 1; i <= m; i++) re(a[i].x), re(a[i].y), a[i].y += n, re(a[i].d);
  f.build(2 * n + 10);
  for (int i = 1; i <= n; i++) {
    f.add(f.st, i, 1);
    f.add(i + n, f.en, 1);
  }
  int l = 1, r = 1e9 + 1, ans = 1e9 + 1;
  while (l <= r) {
    int mid = (l + r) >> 1;
    auto edges = f.edges;
    auto G = f.G;
    auto flow = f.flow;
    for (int i = 1; i <= m; i++)
      if (a[i].d >= l && a[i].d <= mid) f.add(a[i].x, a[i].y, 1);
    if (f.get() == n) {
      ans = mid, r = mid - 1;
      f.edges = edges;
      f.G = G;
      f.flow = flow;
    } else
      l = mid + 1;
  }
  if (ans > 1e9)
    puts("-1");
  else
    printf("%d\n", ans);
}