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 long long INF = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
const int maxn = 2e4 + 10;
const int mod = 1e9 + 7;
const double eps = 1e-6;
int di[8][2] = {{-1, 0},  {1, 0},  {0, -1}, {0, 1},
                {-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
clock_t _st = clock();
inline double now_time() { return (double)(clock() - _st) / CLOCKS_PER_SEC; }
inline long long rd() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 3) + (x << 1) + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}
int n, m;
struct node {
  int x, y, w;
  bool friend operator<(const node& n1, const node& n2) { return n1.w < n2.w; }
} aa[maxn * 10];
int maxflow;
int deep[maxn];
struct Edge {
  int next, to, dis;
} edge[maxn * 20];
int num_edge = -1, head[maxn], cur[maxn];
queue<int> qu;
void add_edge(int from, int to, int dis) {
  edge[++num_edge].next = head[from];
  edge[num_edge].to = to;
  edge[num_edge].dis = dis;
  head[from] = num_edge;
}
void add(int from, int to, int dis) {
  add_edge(from, to, dis);
  add_edge(to, from, 0);
}
void init() {
  num_edge = -1;
  memset(head, -1, sizeof(int) * (n + 10));
}
bool bfs(int s, int t) {
  memset(deep, 0x7f, sizeof(deep));
  while (!qu.empty()) qu.pop();
  for (int i = 0; i <= n + 1; i++) cur[i] = head[i];
  deep[s] = 0;
  qu.push(s);
  while (!qu.empty()) {
    int now = qu.front();
    qu.pop();
    for (int i = head[now]; i != -1; i = edge[i].next) {
      if (deep[edge[i].to] > inf && edge[i].dis) {
        deep[edge[i].to] = deep[now] + 1;
        qu.push(edge[i].to);
      }
    }
  }
  if (deep[t] < inf) return 1;
  return 0;
}
int dfs(int now, int t, int limit) {
  if (!limit || now == t) return limit;
  int flow = 0, f;
  for (int i = cur[now]; i != -1; i = edge[i].next) {
    cur[now] = i;
    if (deep[edge[i].to] == deep[now] + 1 &&
        (f = dfs(edge[i].to, t, min(limit, edge[i].dis)))) {
      flow += f;
      limit -= f;
      edge[i].dis -= f;
      edge[i ^ 1].dis += f;
      if (!limit) break;
    }
  }
  return flow;
}
int Dinic(int s, int t) {
  maxflow = 0;
  while (bfs(s, t)) maxflow += dfs(s, t, inf);
  return maxflow;
}
bool check(int x) {
  init();
  for (int i = 1; i <= m; i++) {
    if (aa[i].w > x) break;
    int a = aa[i].x, b = aa[i].y;
    add(a, b, 1);
  }
  for (int i = 1; i <= n / 2; i++) {
    add(0, i, 1);
  }
  for (int i = n / 2 + 1; i <= n; i++) add(i, n + 1, 1);
  return Dinic(0, n + 1) == n / 2;
}
int main() {
  n = rd(), m = rd();
  for (int i = 1; i <= m; i++) {
    aa[i].x = rd(), aa[i].y = rd() + n, aa[i].w = rd();
  }
  n *= 2;
  sort(aa + 1, aa + 1 + m);
  int l = 1, r = inf;
  int ans = -1;
  while (l <= r) {
    int mid = l + r >> 1;
    if (check(mid)) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  printf("%d\n", ans);
}