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 = 9223372036854775807;
const long long MOD = 1000000007;
const long double PI = acos(-1);
inline long long power(long long a, long long b, long long z = MOD) {
  long long res = 1;
  a = (a % z);
  while (b) {
    if (b & 1) res = (res * a) % z;
    a = (a * a) % z;
    b = b >> 1;
  }
  return res;
}
inline long long inverse(long long a, long long z = MOD) {
  return power(a, z - 2, z);
}
long long extend(long long a, long long b, long long &x, long long &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  long long x1, y1;
  long long g = extend(b, a % b, x1, y1);
  x = y1;
  y = x1 - (a / b) * y1;
  return g;
}
class matrix {
 public:
  long long n, aray[5][5], z = MOD;
  matrix(long long n) {
    this->n = n;
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < n; j++) aray[i][j] = (i == j);
    }
  }
  matrix operator+(const matrix &a) {
    matrix ans = matrix(n);
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < n; j++)
        ans.aray[i][j] = (a.aray[i][j] + aray[i][j]) % z;
    }
    return ans;
  }
  matrix operator*(const matrix &a) {
    matrix ans = matrix(n);
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < n; j++) {
        ans.aray[i][j] = 0;
        for (long long k = 0; k < n; k++)
          ans.aray[i][j] = (ans.aray[i][j] + aray[i][k] * a.aray[k][j]) % z;
      }
    }
    return ans;
  }
};
inline matrix mat_power(matrix x, long long b, long long z = MOD) {
  matrix ans = matrix(x.n);
  while (b) {
    if (b & 1) ans = ans * x;
    x = x * x;
    b = b >> 1;
  }
  return ans;
}
vector<vector<int> > edges;
const long long MAX = 2e5 + 1;
vector<int> arr[MAX], visited(MAX, 0), visit(MAX, 1e7);
vector<int> path;
bool mila;
void dfs(int root) {
  visited[root] = 1;
  path.push_back(root);
  if (visit[root] == 0) {
    mila = true;
    return;
  }
  for (auto x : arr[root]) {
    if (mila) return;
    if (!visited[x] && visit[x] == visit[root] - 1) dfs(x);
  }
  if (!mila) return;
  path.pop_back();
  return;
}
bool hopcraft(int m, int n) {
  m++;
  vector<int> aray[2 * n + 1], sat(2 * n + 1, 0);
  for (int i = 0; i < m; i++) {
    int x = edges[i][1], y = edges[i][2];
    y += n;
    aray[x].push_back(y);
    aray[y].push_back(x);
  }
  while (1) {
    for (auto &x : visited) x = 0;
    for (auto &x : arr) x.clear();
    for (auto &x : visit) x = 1e7;
    queue<int> q;
    for (int i = 1; i <= n; i++) {
      if (!sat[i]) {
        visit[i] = 0;
        q.push(i);
      }
    }
    vector<int> f;
    while (q.size()) {
      int vert = q.front();
      q.pop();
      if (vert <= n) {
        for (auto x : aray[vert]) {
          if (visit[x] < visit[vert] + 1) continue;
          arr[x].push_back(vert);
          arr[vert].push_back(x);
          if (visit[x] > visit[vert] + 1) q.push(x);
          visit[x] = visit[vert] + 1;
        }
      } else {
        if (sat[vert]) {
          int x = sat[vert];
          if (visit[x] < visit[vert] + 1) continue;
          arr[x].push_back(vert);
          arr[vert].push_back(x);
          if (visit[x] > visit[vert] + 1) q.push(x);
          visit[x] = visit[vert] + 1;
        } else
          f.push_back(vert);
      }
    }
    if (f.size() == 0) break;
    for (auto x : f) {
      mila = false;
      path.clear();
      dfs(x);
      if (!mila) continue;
      for (int i = 0; i < path.size(); i += 2) {
        sat[path[i]] = path[i + 1];
        sat[path[i + 1]] = path[i];
      }
    }
  }
  int sum = 0;
  for (int i = 1; i <= n; i++) sum += (sat[i] > 0);
  return (sum == n);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  int t = m;
  while (m--) {
    int x, y, z;
    cin >> x >> y >> z;
    edges.push_back({z, x, y});
  }
  sort(edges.begin(), edges.end());
  int left = 0, right = t - 1, mid;
  while (left < right) {
    mid = (left + right) >> 1;
    if (hopcraft(mid, n))
      right = mid;
    else
      left = mid + 1;
  }
  if (hopcraft(left, n))
    cout << edges[left][0] << "\n";
  else
    cout << -1 << "\n";
  return 0;
}