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;
template <class T>
void _R(T &x) {
  cin >> x;
}
void _R(int &x) { scanf("%d", &x); }
void _R(int64_t &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template <class T, class... U>
void R(T &head, U &...tail) {
  _R(head);
  R(tail...);
}
template <class T>
void _W(const T &x) {
  cout << x;
}
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template <class T, class U>
void _W(const pair<T, U> &x) {
  _W(x.first);
  putchar(' ');
  _W(x.second);
}
template <class T>
void _W(const vector<T> &x) {
  for (auto i = x.begin(); i != x.end(); _W(*i++))
    if (i != x.cbegin()) putchar(' ');
}
void W() {}
template <class T, class... U>
void W(const T &head, const U &...tail) {
  _W(head);
  putchar(sizeof...(tail) ? ' ' : '\n');
  W(tail...);
}
int MOD = 1e9 + 7;
void ADD(long long &x, long long v) {
  x = (x + v) % MOD;
  if (x < 0) x += MOD;
}
const int SIZE = 1 << 20;
struct E {
  int k;
  long long c;
  E() {}
  E(int _k, long long _c) : k(_k), c(_c) {}
} es[2000010];
struct Flow {
  int n, m, dis[500010];
  int qq[500010], qr, ql;
  int ptr[500010];
  vector<int> e[500010];
  void init(int _n) {
    n = _n;
    m = 0;
    for (int i = 0; i < n; i++) {
      vector<int> tmp;
      e[i].swap(tmp);
    }
  }
  void add_edge(int a, int b, long long c) {
    e[a].push_back(m);
    es[m] = E(b, c);
    m++;
    e[b].push_back(m);
    es[m] = E(a, 0);
    m++;
  }
  bool BFS() {
    memset(dis, -1, n * sizeof(int));
    ql = qr = 0;
    qq[qr++] = 0;
    dis[0] = 0;
    while (ql != qr && dis[n - 1] == -1) {
      int p = qq[ql++];
      for (auto &it : e[p]) {
        E ee = es[it];
        if (ee.c == 0 || dis[ee.k] != -1) continue;
        dis[ee.k] = dis[p] + 1;
        qq[qr++] = ee.k;
      }
    }
    return dis[n - 1] != -1;
  }
  long long go(int p, long long c) {
    if (p == n - 1) return c;
    long long ret = 0, tmp;
    for (int &i = ptr[p]; i < ((int)(e[p]).size()); i++) {
      auto it = e[p][i];
      E &ee = es[it];
      if (ee.c == 0 || dis[p] + 1 != dis[ee.k]) continue;
      tmp = go(ee.k, min(c - ret, (long long)ee.c));
      ret += tmp;
      ee.c -= tmp;
      es[it ^ 1].c += tmp;
      if (ret == c) break;
    }
    if (ret == 0) dis[p] = -1;
    return ret;
  }
  long long maxflow() {
    long long ret = 0;
    while (BFS()) {
      memset(ptr, 0, n * sizeof(int));
      ret += go(0, 1LL << 60);
    }
    return ret;
  }
} flow;
struct EDGE {
  int x, y, v;
  EDGE(int x = 0, int y = 0, int v = 0) : x(x), y(y), v(v) {}
  bool operator<(const EDGE &b) const { return v < b.v; }
} AA[SIZE];
int N, M;
bool valid(int mm) {
  flow.init(2 * N + 2);
  for (int i = (1); i <= (N); ++i) {
    flow.add_edge(0, i, 1);
    flow.add_edge(i + N, N * 2 + 1, 1);
  }
  for (int i = 0; i < (mm); ++i) {
    flow.add_edge(AA[i].x, AA[i].y + N, 1);
  }
  return flow.maxflow() == N;
}
void solve() {
  sort(AA, AA + M);
  int ll = N, rr = M + 1;
  while (ll < rr) {
    int mm = (ll + rr) / 2;
    if (valid(mm))
      rr = mm;
    else
      ll = mm + 1;
  }
  int an;
  if (ll == M + 1)
    an = -1;
  else
    an = AA[ll - 1].v;
  W(an);
}
void input() {
  R(N, M);
  for (int i = 0; i < (M); ++i) R(AA[i].x, AA[i].y, AA[i].v);
}
int main() {
  input();
  solve();
  return 0;
}