#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(time(0));
const long long inf = 0x3f3f3f3f3f3f3f3fLL;
const int N = 2e4 + 5;
const int MOD = 1e9 + 7;
struct FlowEdge {
  int u, v;
  long long cap, flow = 0;
  FlowEdge(int u, int v, long long cap) : u(u), v(v), cap(cap) {}
};
struct Dinic {
  vector<FlowEdge> e;
  vector<vector<int>> g;
  int n, idx = 0, s, t;
  vector<int> lvl, par;
  queue<int> q;
  Dinic(int n, int s, int t) : n(n), s(s), t(t) {
    g.resize(n);
    lvl.resize(n);
    par.resize(n);
  }
  void addEdge(int u, int v, long long cap) {
    e.emplace_back(u, v, cap);
    e.emplace_back(v, u, 0);
    g[u].push_back(idx);
    g[v].push_back(idx + 1);
    idx += 2;
  }
  bool bfs() {
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      for (auto i : g[u]) {
        if (lvl[e[i].v] == -1 && e[i].cap - e[i].flow > 0) {
          lvl[e[i].v] = lvl[u] + 1;
          q.push(e[i].v);
        }
      }
    }
    return lvl[t] != -1;
  }
  int dfs(int u, long long pushed) {
    if (pushed == 0) return 0;
    if (u == t) return pushed;
    for (int& idx = par[u]; idx < (int)g[u].size(); idx++) {
      int i = g[u][idx];
      int v = e[i].v;
      if (lvl[u] + 1 != lvl[v] || e[i].cap - e[i].flow < 1) continue;
      int tr = dfs(e[i].v, min(pushed, e[i].cap - e[i].flow));
      if (tr == 0) continue;
      e[i].flow += tr;
      e[i ^ 1].flow -= tr;
      return tr;
    }
    return 0;
  }
  long long getMaxflow() {
    long long max_flow = 0;
    while (true) {
      fill(lvl.begin(), lvl.end(), -1);
      lvl[s] = 0;
      q.push(s);
      if (!bfs()) break;
      fill(par.begin(), par.end(), 0);
      while (long long pushed = dfs(s, inf)) {
        max_flow += pushed;
      }
    }
    return max_flow;
  }
};
int n, m;
vector<tuple<int, int, int>> e;
bool check(int x) {
  Dinic o(2 * n + 2, 0, 2 * n + 1);
  for (auto& [u, v, w] : e) {
    if (w <= x) {
      o.addEdge(u, v, 1);
    }
  }
  for (int i = 1; i <= n; i++) {
    o.addEdge(0, i, 1);
    o.addEdge(n + i, 2 * n + 1, 1);
  }
  return o.getMaxflow() == n;
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  int u, v, w;
  for (int i = 0; i < m; i++) {
    cin >> u >> v >> w;
    e.emplace_back(u, n + v, w);
  }
  int l = 1, r = (int)1e9;
  if (!check(r)) {
    cout << -1;
    return 0;
  }
  while (l < r) {
    int mid = l + r >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l;
  return 0;
}
