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;
double pi = acos(-1);
template <typename T>
inline T read(T& x) {
  char c11 = getchar(), ob = 0;
  x = 0;
  while (c11 ^ '-' && !isdigit(c11)) c11 = getchar();
  if (c11 == '-') c11 = getchar(), ob = 1;
  while (isdigit(c11)) x = x * 10 + c11 - '0', c11 = getchar();
  if (ob) x = -x;
  return x;
}
template <typename T>
vector<T>& operator--(vector<T>& v) {
  for (auto& i : v) --i;
  return v;
}
template <typename T>
vector<T>& operator++(vector<T>& v) {
  for (auto& i : v) ++i;
  return v;
}
template <typename T>
ostream& operator<<(ostream& os, vector<T>& v) {
  for (auto& i : v) os << i << ' ';
  return os;
}
template <typename T, typename U>
istream& operator>>(istream& is, pair<T, U>& p) {
  is >> p.first >> p.second;
  return is;
}
template <typename T, typename U>
ostream& operator<<(ostream& os, pair<T, U>& p) {
  os << p.first << ' ' << p.second;
  return os;
}
template <typename T, typename U>
pair<T, U> operator-(pair<T, U> a, pair<T, U> b) {
  return make_pair(a.first - b.first, a.second - b.second);
}
template <typename T, typename U>
pair<T, U> operator+(pair<T, U> a, pair<T, U> b) {
  return make_pair(a.first + b.first, a.second + b.second);
}
template <unsigned long T>
bitset<T>& operator++(bitset<T>& v) {
  v = v.to_ulong() + 1;
  return v;
}
template <unsigned long T>
bitset<T>& operator--(bitset<T>& v) {
  v = v.to_ulong() - 1;
  return v;
}
template <unsigned long T, typename U>
bool operator<(bitset<T>& v, U p) {
  return v.to_ulong() < p;
}
template <unsigned long T, typename U>
bool operator>(bitset<T>& v, U p) {
  return v.to_ulong() > p;
}
template <unsigned int T>
bitset<T>& operator++(bitset<T>& v) {
  v = v.to_ulong() + 1;
  return v;
}
template <unsigned int T>
bitset<T>& operator--(bitset<T>& v) {
  v = v.to_ulong() - 1;
  return v;
}
template <unsigned int T, typename U>
bool operator<(bitset<T>& v, U p) {
  return v.to_ulong() < p;
}
template <unsigned int T, typename U>
bool operator>(bitset<T>& v, U p) {
  return v.to_ulong() > p;
}
template <typename T, typename U>
void umin(T& a, U b) {
  if (a > b) a = b;
}
template <typename T, typename U>
void umax(T& a, U b) {
  if (a < b) a = b;
}
const int N = 202000;
const int inf = 1e9 + 90999;
int bol, st, ed;
int head[N], lv[N];
int a[200010], b[200010], c[200010];
struct node {
  int e, flow, nex;
  node() {}
  node(int e, int flow, int nex) : e(e), flow(flow), nex(nex) {}
} edg[N << 2];
inline void add(int s, int e, int flow) {
  edg[bol] = node(e, flow, head[s]);
  head[s] = bol++;
  edg[bol] = node(s, 0, head[e]);
  head[e] = bol++;
}
bool BFS() {
  queue<int> q;
  memset(lv, 0, sizeof lv);
  lv[st] = 1;
  q.push(st);
  while (!q.empty()) {
    int s = q.front();
    q.pop();
    for (int i = head[s]; ~i; i = edg[i].nex)
      if (edg[i].flow) {
        int e = edg[i].e;
        if (!lv[e]) lv[e] = lv[s] + 1, q.push(e);
      }
  }
  if (!lv[ed]) return false;
  return true;
}
int dfs(int s, int ff) {
  if (s == ed || !ff) return ff;
  int ans = 0;
  for (int i = head[s]; ~i; i = edg[i].nex) {
    int e = edg[i].e;
    if (lv[e] == lv[s] + 1) {
      int u = dfs(e, min(edg[i].flow, ff - ans));
      ans += u;
      edg[i].flow -= u;
      edg[i ^ 1].flow += u;
      if (ff == ans) break;
    }
  }
  if (!ans) lv[s] = -1;
  return ans;
}
int dinic() {
  int ans = 0;
  while (BFS()) ans += dfs(st, inf);
  return ans;
}
int n, m, ans = -1;
bool check(int x) {
  bol = 0;
  memset(head, -1, sizeof head);
  for (int i = 1; i <= n; i++) {
    add(st, i, 1);
    add(i + n, ed, 1);
  }
  for (int i = 0; i < m; i++)
    if (c[i] <= x) add(a[i], b[i] + n, 1);
  int num = dinic();
  return (num == n);
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  st = 2 * n + 1;
  ed = st + 1;
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  int l = 0, r = inf;
  while (l < r) {
    int mid = l + r >> 1;
    if (check(mid) == 0) {
      l = mid + 1;
    } else {
      r = mid;
    }
  }
  if (r == inf)
    cout << -1 << endl;
  else
    cout << r << endl;
}