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>
inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << '(' << p.first << ',' << p.second << ')';
  return os;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  os << '{';
  for (int i = 0; i < (int)v.size(); i++) {
    if (i) {
      os << ',';
    }
    os << v[i];
  }
  os << '}';
  return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &...args) {
  cerr << " " << x;
  debugg(args...);
}
struct Setup {
  Setup() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  }
} __Setup;
using ll = long long;
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
template <class Cap>
class Dinic {
 private:
  struct edge {
    int to, rev;
    Cap cap;
  };
  vector<vector<edge>> G;
  vector<int> level, iter;
  vector<pair<int, int>> pos;
  void bfs(int s) {
    fill((level).begin(), (level).end(), -1);
    queue<int> que;
    level[s] = 0;
    que.emplace(s);
    while (!que.empty()) {
      int v = que.front();
      que.pop();
      for (const auto &e : G[v]) {
        if (e.cap > 0 && level[e.to] < 0) {
          level[e.to] = level[v] + 1;
          que.emplace(e.to);
        }
      }
    }
  }
  Cap dfs(int v, int t, Cap f) {
    if (v == t) {
      return f;
    }
    for (int &i = iter[v]; i < (int)G[v].size(); i++) {
      edge &e = G[v][i];
      if (e.cap > 0 && level[v] < level[e.to]) {
        auto d = dfs(e.to, t, min(f, e.cap));
        if (d > 0) {
          e.cap -= d;
          G[e.to][e.rev].cap += d;
          return d;
        }
      }
    }
    return 0;
  }

 public:
  Dinic(int n) : G(n), level(n), iter(n) {}
  int add_edge(int from, int to, Cap cap) {
    int n = (int)G.size();
    assert(0 <= from && from < n);
    assert(0 <= to && to < n);
    assert(0 <= cap);
    int m = (int)pos.size();
    pos.emplace_back(from, (int)G[from].size());
    G[from].emplace_back(edge{to, (int)G[to].size(), cap});
    G[to].emplace_back(edge{from, (int)G[from].size() - 1, 0});
    return m;
  }
  struct res_edge {
    int from, to;
    Cap cap, flow;
  };
  res_edge get_edge(int i) {
    int m = (int)pos.size();
    assert(0 <= i && i < m);
    auto e = G[pos[i].first][pos[i].second];
    auto re = G[e.to][e.rev];
    return res_edge{pos[i].first, e.to, e.cap + re.cap, re.cap};
  }
  vector<res_edge> edges() {
    int m = (int)pos.size();
    vector<res_edge> res;
    for (int i = 0; i < m; i++) {
      res.emplace_back(get_edge(i));
    }
    return res;
  }
  void change_edge(int i, Cap new_cap, Cap new_flow) {
    int m = (int)pos.size();
    assert(0 <= i && i < m);
    assert(0 <= new_flow && new_flow <= new_cap);
    auto &e = G[pos[i].first][pos[i].second];
    auto &re = G[e.to][e.rev];
    e.cap = new_cap - new_flow;
    re.cap = new_flow;
  }
  Cap max_flow(int s, int t, Cap limit = numeric_limits<Cap>::max()) {
    Cap flow = 0;
    while (1) {
      bfs(s);
      if (level[t] < 0) {
        return flow;
      }
      fill((iter).begin(), (iter).end(), 0);
      Cap f;
      while ((f = dfs(s, t, limit)) > 0) {
        flow += f;
      }
    }
  }
};
struct edge {
  int from, to, day;
  edge() = default;
  edge(int from, int to, int day) : from(from), to(to), day(day) {}
};
int main() {
  int N, M;
  cin >> N >> M;
  vector<edge> es(M);
  for (int i = 0; i < M; i++) {
    int u, v, d;
    cin >> u >> v >> d;
    es[i] = edge(u - 1, v + N - 1, d);
  }
  sort((es).begin(), (es).end(),
       [](const edge &a, const edge &b) { return (a.day < b.day); });
  auto f = [&](int x) {
    Dinic<int> mf(2 * N + 2);
    int s = 2 * N;
    int t = s + 1;
    vector<bool> used(2 * N, 0);
    for (int i = 0; i < M; i++) {
      if (es[i].day > x) {
        break;
      }
      mf.add_edge(es[i].from, es[i].to, 1);
      if (!used[es[i].from]) {
        mf.add_edge(s, es[i].from, 1);
        used[es[i].from] = true;
      }
      if (!used[es[i].to]) {
        mf.add_edge(es[i].to, t, 1);
        used[es[i].to] = true;
      }
    }
    auto ans = mf.max_flow(s, t);
    return (ans == N);
  };
  int ok = es.back().day + 1, ng = 0;
  while (ok - ng > 1) {
    int mid = (ok + ng) >> 1;
    (f(mid) ? ok : ng) = mid;
  }
  cout << (ok >= es.back().day + 1 ? -1 : ok) << "\n";
}