#include <bits/stdc++.h>
using namespace std;
using lli = long long int;
const int MOD = 1000000007;
const int MOD1 = 998244353;
const int maxn = 100010;
const int lim = (int)1e9;
struct flowEdge {
  int v, u;
  lli cap, flow = 0ll;
  flowEdge(int v, int u, lli cap) : v(v), u(u), cap(cap) {}
};
struct Dinic {
  const lli flow_inf = 1e18;
  vector<flowEdge> edges;
  vector<vector<int> > adj;
  int n, m = 0;
  int s, t;
  vector<int> level, ptr;
  queue<int> q;
  Dinic(int n, int s, int t) : n(n), s(s), t(t) {
    adj.resize(n);
    level.resize(n);
    ptr.resize(n);
  }
  void addEdge(int v, int u, lli cap) {
    edges.emplace_back(v, u, cap);
    edges.emplace_back(u, v, 0);
    adj[v].push_back(m);
    adj[u].push_back(m + 1);
    m += 2;
  }
  bool bfs() {
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      for (auto id : adj[v]) {
        if (edges[id].cap - edges[id].flow < 1) continue;
        if (level[edges[id].u] != -1) continue;
        level[edges[id].u] = level[v] + 1;
        q.push(edges[id].u);
      }
    }
    return level[t] != -1;
  }
  lli dfs(int v, lli pushed) {
    if (pushed == 0ll) return 0ll;
    if (v == t) return pushed;
    for (int &cid = ptr[v]; cid < (int)adj[v].size(); ++cid) {
      int id = adj[v][cid];
      int u = edges[id].u;
      if (level[v] + 1 != level[u] || edges[id].cap - edges[id].flow < 1)
        continue;
      lli tr = dfs(u, min(pushed, edges[id].cap - edges[id].flow));
      if (tr == 0ll) continue;
      edges[id].flow += tr;
      edges[id ^ 1].flow -= tr;
      return tr;
    }
    return 0ll;
  }
  lli flow() {
    lli f = 0ll;
    while (true) {
      fill(level.begin(), level.end(), -1);
      level[s] = 0;
      q.push(s);
      if (!bfs()) break;
      fill(ptr.begin(), ptr.end(), 0);
      while (lli pushed = dfs(s, flow_inf)) f += pushed;
    }
    return f;
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n = 0, m = 0, x = 0, y = 0, w = 0;
  cin >> n >> m;
  vector<pair<int, pair<int, int> > > v;
  for (int i = 0; i < m; ++i) {
    cin >> x >> y >> w;
    --x;
    --y;
    v.push_back({w, {x, y}});
  }
  sort(v.begin(), v.end());
  int l = n, r = m;
  while (l != r) {
    int mid = (l + r) / 2;
    Dinic g(2 * n + 2, 2 * n, 2 * n + 1);
    for (int i = 0; i < n; ++i) {
      g.addEdge(2 * n, i, 1);
      g.addEdge(n + i, 2 * n + 1, 1);
    }
    for (int i = 0; i <= mid; ++i)
      g.addEdge(v[i].second.first, n + v[i].second.second, 1);
    if (g.flow() == n)
      r = mid;
    else
      l = mid + 1;
  }
  if (l >= m)
    cout << "-1";
  else
    cout << v[l].first;
}
