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;
inline string toString(int x) {
  string ans = "";
  bool negtive = x < 0;
  while (x) ans.push_back(x % 10 + '0'), x /= 10;
  if (ans.size() == 0) ans = "0";
  if (negtive) ans.push_back('-');
  for (int i = 0; i < ans.size() / 2; i++)
    swap(ans[i], ans[ans.size() - 1 - i]);
  return ans;
}
inline int toInt(const string &str) {
  int ans = 0;
  bool negtive = str[0] == '-';
  for (int i = negtive; i < str.size(); i++) ans = ans * 10 + str[i] - '0';
  if (negtive) ans *= -1;
  return ans;
}
inline int gcd(int x, int y) { return x ? gcd(y % x, x) : y; }
const int mod = 1e9 + 7;
inline signed rd() {
  signed ans = 0;
  char last = ' ', ch = getchar();
  while (!(ch >= '0' && ch <= '9')) last = ch, ch = getchar();
  while (ch >= '0' && ch <= '9')
    ans = (ans << 3) + (ans << 1) + ch - '0', ch = getchar();
  return ans;
}
inline int QPow(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1) ans = ans * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}
inline int QPow(int a, int b, const int &mod) {
  int ans = 1;
  while (b) {
    if (b & 1) ans = ans * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}
long long exgcd(long long a, long long b, long long &x, long long &y) {
  long long d = a;
  if (b == 0)
    x = 1, y = 0;
  else {
    d = exgcd(b, a % b, y, x), y -= a / b * x;
  }
  return d;
}
const long double PI = 3.141592653589793115997963468544185161590576171875;
using namespace std;
const int inf = 10000007;
const int maxn = 20005;
int n, m, i, k, s, t, ans, tot, x, y;
int q[maxn], head[maxn], nex[maxn * 12], b[maxn * 12], c[maxn * 12], cur[maxn],
    d[maxn];
inline void Plus(int x, int y, int z) {
  nex[++tot] = head[x];
  head[x] = tot;
  b[tot] = y;
  c[tot] = z;
}
inline void add(int x, int y, int z) {
  Plus(x, y, z);
  Plus(y, x, 0);
}
inline int bfs() {
  int l, r, x, i;
  l = 0, r = 1;
  q[r] = s;
  memset(d, 0, sizeof(d));
  d[s] = 1;
  while (l < r) {
    x = q[++l];
    for (i = head[x]; i != -1; i = nex[i]) {
      if ((d[b[i]] == 0) && (c[i] > 0)) {
        d[b[i]] = d[x] + 1;
        q[++r] = b[i];
      }
    }
  }
  if (d[t] > 0)
    return 1;
  else
    return 0;
}
inline int dfs(int x, int f) {
  if (x == t) return f;
  for (int &i = cur[x]; i != -1; i = nex[i]) {
    if ((d[b[i]] == d[x] + 1) && (c[i] > 0)) {
      int di = dfs(b[i], min(f, c[i]));
      if (di > 0) {
        c[i] -= di;
        c[i ^ 1] += di;
        return di;
      }
    }
  }
  return 0;
}
pair<int, pair<signed, signed> > p[maxn * 5];
inline bool check(int x) {
  tot = -1;
  ans = 0;
  memset(nex, -1, sizeof nex);
  memset(head, -1, sizeof head);
  s = 0, t = 2 * n + 1;
  for (int i = 1; i <= n; i++) add(s, i, 1);
  for (int i = n + 1; i <= 2 * n; i++) add(i, t, 1);
  for (int i = 1; i <= m; i++)
    if (p[i].first <= x)
      add(p[i].second.first, p[i].second.second + n, 1);
    else
      break;
  while (bfs()) {
    for (int i = s; i <= t; i++) cur[i] = head[i];
    while (int di = dfs(s, inf)) ans += di;
  }
  return ans == n;
}
signed main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int x, y, z, d, t;
  n = rd(), m = rd();
  for (int i = 1; i <= m; i++) {
    x = rd(), y = rd(), z = rd();
    p[i] = {z, {x, y}};
  }
  sort(p + 1, p + 1 + m);
  int lef = 1, rig = 1000000000;
  int md = (lef + rig) >> 1;
  while (lef <= rig) {
    md = (lef + rig) >> 1;
    if (check(md))
      rig = md - 1;
    else
      lef = md + 1;
  }
  if (check(md - 1)) md--;
  if (check(md) == 0) md++;
  if (md > 1000000000)
    cout << -1;
  else
    cout << md << endl;
  return 0;
}