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>
#pragma GCC optimize("O3")
using namespace std;
template <class A, class B, class C>
struct triple {
  A first;
  B second;
  C third;
  bool operator<(const triple &t) const {
    if (first != t.first) return first < t.first;
    if (second != t.second) return second < t.second;
    return third < t.third;
  }
};
const long double EPS = 0.000000001;
const long double INFD32 = 1000000000;
const long long INF64 = 1000000000000000000;
const long long INF32 = 1000000000;
const long long one = 1;
bool iss_ll(long long __1, long long __2) { return INF64 / __1 >= __2; }
bool equal(double _1, double _2) { return (abs(_1 - _2) < EPS); }
bool iss_sqr(long long &__1) {
  long long __2 = sqrt((long double)__1);
  return __2 * __2 == __1;
};
bool iss_prime(long long __1) {
  for (long long i = 2; i * i <= __1; ++i)
    if (!(__1 % i)) return 0;
  return 1;
}
long long gcd(long long __1, long long __2) {
  while (__1 && __2)
    if (__1 > __2)
      __1 = __1 % __2;
    else
      __2 = __2 % __1;
  return __1 + __2;
}
long long lcm(long long __1, long long __2) {
  return __1 * __2 / (gcd(__1, __2));
}
long long sq_cube(long long x) {
  if (x == 0) return 1;
  long long l = 0, r = 2e6;
  while (l + 1 < r) {
    long long m = (l + r) / 2;
    if (m * m * m > x)
      r = m;
    else
      l = m;
  }
  return l;
}
long long bp(long long a, long long b, long long md) {
  long long res = 1;
  while (b) {
    if (b & 1) res *= a;
    a *= a;
    a %= md;
    res %= md;
    b >>= 1;
  }
  return res;
}
long long inv(long long x, long long md) { return bp(x, md - 2, md); }
void update_by_mod(long long &x, long long md) {
  x %= md;
  if (x < 0) x += md;
}
void update_by_mod(long long &x, long long &y, long long md) {
  update_by_mod(x, md);
  update_by_mod(y, md);
}
void update_by_mod(long long &x, long long &y, long long &z, long long md) {
  update_by_mod(x, md);
  update_by_mod(y, md);
  update_by_mod(z, md);
}
void update_by_mod(long long &x, long long &y, long long &z, long long &a,
                   long long md) {
  update_by_mod(x, md);
  update_by_mod(y, md);
  update_by_mod(z, md);
  update_by_mod(a, md);
}
vector<long long> z_function(string &s) {
  long long n = ((long long)((s).size()));
  vector<long long> ans(n, 0);
  long long l = 0, r = 0;
  for (long long i = 1; i < n; ++i) {
    ans[i] = min(r - i + 1, ans[i - l]);
    while (ans[i] + i < n && s[ans[i]] == s[i + ans[i]]) {
      ++ans[i];
    }
    if (i + ans[i] - 1 > r) {
      l = i, r = i + ans[i] - 1;
    }
    r = max(r, i);
  }
  return ans;
}
template <class T>
T sqr(T x) {
  return x * x;
}
pair<long long, long long> operator+(pair<long long, long long> x,
                                     pair<long long, long long> y) {
  return {x.first + y.first, x.second + y.second};
}
long long vis[500500];
long long n;
vector<long long> G[500500];
pair<long long, long long> dfs(long long v) {
  if (vis[v]) return {0, 0};
  vis[v] = 1;
  pair<long long, long long> add;
  if (v <= n)
    add = {1, 0};
  else
    add = {0, 1};
  for (long long i : G[v]) {
    add = add + dfs(i);
  }
  return add;
}
void solve() {
  long long m;
  cin >> n >> m;
  vector<pair<long long, pair<long long, long long>>> VEC;
  for (long long i = 0; i < m; ++i) {
    long long x, y, d;
    cin >> x >> y >> d;
    y += n;
    VEC.push_back({d, {x, y}});
  }
  sort((VEC).begin(), (VEC).end());
  long long l = 0, r = VEC.size();
  while (l + 1 < r) {
    for (long long i = 1; i <= 2 * n; ++i) G[i].clear(), vis[i] = 0;
    long long m = (l + r) / 2;
    for (long long i = 0; i <= m; ++i) {
      long long x = VEC[i].second.first, y = VEC[i].second.second;
      G[x].push_back(y);
      G[y].push_back(x);
    }
    bool fg = 1;
    for (long long i = 1; i <= 2 * n; ++i) {
      if (!vis[i]) {
        pair<long long, long long> x = dfs(i);
        if (x.first != x.second) fg = 0;
      }
    }
    if (fg)
      r = m;
    else
      l = m;
  }
  if (r != VEC.size())
    cout << VEC[r].first << "\n";
  else
    cout << -1 << "\n";
}
signed main(void) {
  srand(time(0));
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  for (long long i = 1; i <= t; ++i) {
    solve();
  }
  return 0;
}