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 = 2e5 + 5, M = 1e3 + 5;
const long long INF = 1e18 + 5;
inline long long read() {
  long long sum = 0, fh = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') fh = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  return sum * fh;
}
inline int read2() {
  char c = getchar();
  while (c != 'a' && c != 'b' && c != 'c' && c != '?') c = getchar();
  return c == '?' ? 0 : c - 'a' + 1;
}
inline int read3() {
  char c = getchar();
  while (c < 'a' || c > 'z') {
    c = getchar();
  }
  return c - 'a';
}
inline void write(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
inline int gcd(int x, int y) { return y == 0 ? x : gcd(y, x % y); }
inline long long ab(long long x) { return x < 0 ? -x : x; }
inline long long fpow(long long qwe, long long asd, long long zxc) {
  if (asd < 0) return 0;
  long long a = qwe, b = 1, c = asd;
  while (c) {
    if (c & 1) b = b * a % zxc;
    a = a * a % zxc;
    c >>= 1;
  }
  return b;
}
struct miaow {
  int f, t, c, l;
  miaow(int f = 0, int t = 0, int c = 0, int l = 0) : f(f), t(t), c(c), l(l) {}
} imap[N * 2 + 5];
int str[N * 2 + 5] = {0}, cou = 2;
inline void jb(int f, int t, int c = 0) {
  imap[cou] = miaow(f, t, c, str[f]);
  str[f] = cou++;
  imap[cou] = miaow(t, f, 0, str[t]);
  str[t] = cou++;
}
int n, m, s, t;
int d[N];
inline int bfs() {
  queue<int> qwe;
  for (int i = 1; i <= n * 2 + 2; ++i) d[i] = 0;
  d[s] = 1;
  qwe.push(s);
  while (!qwe.empty()) {
    int x = qwe.front();
    qwe.pop();
    for (int i = str[x]; i; i = imap[i].l) {
      if (!imap[i].c) continue;
      int t = imap[i].t;
      if (!d[t]) {
        d[t] = d[x] + 1;
        qwe.push(t);
      }
    }
  }
  return d[t];
}
int str2[N];
inline int dfs(int o, int f) {
  if (o == t) return f;
  int fl = 0;
  for (int& i = str2[o]; i; i = imap[i].l) {
    int t = imap[i].t;
    if (d[t] != d[o] + 1 || !imap[i].c) continue;
    int ff = dfs(t, imap[i].c);
    if (!ff) continue;
    imap[i].c -= ff, imap[i ^ 1].c += ff;
    fl += ff;
    f -= ff;
    if (!f) break;
  }
  return fl;
}
inline int dinic() {
  int sum = 0;
  while (bfs()) {
    for (int i = 1; i <= n * 2 + 2; ++i) str2[i] = str[i];
    sum += dfs(s, 1e9);
  }
  return sum;
}
struct meow {
  int f, t, c, o;
  meow(int f = 0, int t = 0, int c = 0, int o = 0) : f(f), t(t), c(c), o(o) {}
  inline bool operator<(const meow& qwe) const { return c < qwe.c; }
} bj[N];
int main() {
  n = read(), m = read();
  for (int i = 1; i <= m; ++i) {
    int x = read(), y = read() + n, z = read();
    bj[i] = meow(x, y, z, 0);
  }
  sort(bj + 1, bj + m + 1);
  s = n * 2 + 1, t = n * 2 + 2;
  for (int i = 1; i <= n; ++i) {
    jb(s, i, 1);
    jb(i + n, t, 1);
  }
  for (int i = 1; i <= m; ++i) {
    bj[i].o = cou;
    jb(bj[i].f, bj[i].t, 1);
  }
  int imi = 2e9, l = 1, r = m;
  while (l <= r) {
    int mid = (l + r) >> 1;
    for (int i = 2; i <= bj[mid].o + 1; i += 2) {
      imap[i].c = 1;
      imap[i ^ 1].c = 0;
    }
    for (int i = bj[mid].o + 2; i < cou; ++i) imap[i].c = 0;
    int x = dinic();
    if (x == n) {
      r = mid - 1;
      imi = min(imi, bj[mid].c);
    } else {
      l = mid + 1;
    }
  }
  if (imi > 1e9)
    puts("-1");
  else
    write(imi), putchar('\n');
}