#include <bits/stdc++.h>
using namespace std;
const long long M = 2e5 + 10;
struct Edge {
  long long u, v, cap, flow = 0;
  Edge(long long u, long long v, long long cap) : u(u), v(v), cap(cap) {}
};
class Dinic {
 public:
  vector<Edge> edges;
  vector<vector<long long>> G;
  long long n, m = 0, s, t;
  vector<long long> H, ptr;
  queue<long long> q;
  Dinic(long long n, long long s, long long t) : n(n), s(s), t(t) {
    G.resize(n + 1);
    H = ptr = vector<long long>(n + 1);
  }
  void add(long long u, long long v, long long cap) {
    edges.emplace_back(u, v, cap);
    edges.emplace_back(v, u, 0);
    G[u].push_back(m++);
    G[v].push_back(m++);
  }
  bool bfs() {
    while (!q.empty()) {
      long long u = q.front();
      q.pop();
      for (long long v : G[u]) {
        long long to = edges[v].v;
        long long gap = edges[v].cap - edges[v].flow;
        if (H[to] != -1 or gap < 1) continue;
        H[to] = H[u] + 1, q.push(to);
      }
    }
    return H[t] != -1;
  }
  long long dfs(long long u, long long X) {
    if (!X) return 0;
    if (u == t) return X;
    for (long long &i = ptr[u]; i < (long long)(G[u]).size(); ++i) {
      long long v = G[u][i], to = edges[v].v;
      long long gap = edges[v].cap - edges[v].flow;
      if (H[to] != (H[u] + 1) or gap < 1) continue;
      long long Y = dfs(to, min(X, gap));
      if (!Y) continue;
      edges[v].flow += Y;
      edges[v ^ 1].flow -= Y;
      return Y;
    }
    return 0;
  }
  long long flow() {
    long long ans = 0;
    while (true) {
      fill(H.begin(), H.end(), -1);
      H[s] = 0, q.push(s);
      if (!bfs()) break;
      fill(ptr.begin(), ptr.end(), 0);
      while (long long X = dfs(s, (long long)(1e18))) ans += X;
    }
    return ans;
  }
};
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long n, m;
  cin >> n >> m;
  vector<pair<long long, pair<long long, long long>>> edges;
  vector<long long> cand;
  for (long long i = 0; i < m; ++i) {
    long long u, v, w;
    cin >> u >> v >> w;
    edges.push_back({w, {u, v}});
    cand.push_back(w);
  }
  sort((cand).begin(), (cand).end());
  cand.erase(unique((cand).begin(), (cand).end()), cand.end());
  long long lo = 0, hi = (long long)(cand).size() - 1, mid, ans = -1;
  long long s = 0, t = 2 * n + 1;
  while (lo <= hi) {
    mid = (lo + hi) >> 1;
    Dinic dinic(2 * n + 2, s, t);
    for (long long i = 1; i <= n; ++i) {
      dinic.add(s, i, 1);
      dinic.add(i + n, t, 1);
    }
    for (auto [w, p] : edges)
      if (w <= cand[mid]) dinic.add(p.first, p.second + n, 1);
    long long x = dinic.flow();
    if (x == n)
      hi = mid - 1, ans = cand[mid];
    else
      lo = mid + 1;
  }
  cout << ans;
  return 0;
}
