#include <bits/stdc++.h>
using namespace std;
class BGraph {
  long long m, n;
  std::list<long long> *adj;
  long long *pair_u, *pair_v, *dist;

 public:
  BGraph(long long m, long long n);
  void addEdge(long long u, long long v);
  void deleteEdge(long long u);
  bool bfs();
  bool dfs(long long u);
  long long hopcroftKarpAlgorithm();
};
long long BGraph::hopcroftKarpAlgorithm() {
  pair_u = new long long[m + 1];
  pair_v = new long long[n + 1];
  dist = new long long[m + 1];
  for (long long u = 0; u <= m; u++) pair_u[u] = 0;
  for (long long v = 0; v <= n; v++) pair_v[v] = 0;
  long long result = 0;
  while (bfs()) {
    for (long long u = 1; u <= m; u++)
      if (pair_u[u] == 0 && dfs(u)) result++;
  }
  return result;
}
bool BGraph::bfs() {
  std::queue<long long> q;
  for (long long u = 1; u <= m; u++) {
    if (pair_u[u] == 0) {
      dist[u] = 0;
      q.push(u);
    } else
      dist[u] = INT_MAX;
  }
  dist[0] = INT_MAX;
  while (!q.empty()) {
    long long u = q.front();
    q.pop();
    if (dist[u] < dist[0]) {
      std::list<long long>::iterator it;
      for (it = adj[u].begin(); it != adj[u].end(); ++it) {
        long long v = *it;
        if (dist[pair_v[v]] == INT_MAX) {
          dist[pair_v[v]] = dist[u] + 1;
          q.push(pair_v[v]);
        }
      }
    }
  }
  return (dist[0] != INT_MAX);
}
bool BGraph::dfs(long long u) {
  if (u != 0) {
    std::list<long long>::iterator it;
    for (it = adj[u].begin(); it != adj[u].end(); ++it) {
      long long v = *it;
      if (dist[pair_v[v]] == dist[u] + 1) {
        if (dfs(pair_v[v]) == true) {
          pair_v[v] = u;
          pair_u[u] = v;
          return true;
        }
      }
    }
    dist[u] = INT_MAX;
    return false;
  }
  return true;
}
BGraph::BGraph(long long m, long long n) {
  this->m = m;
  this->n = n;
  adj = new std::list<long long>[m + 1];
}
void BGraph::addEdge(long long u, long long v) { adj[u].push_back(v); }
void BGraph::deleteEdge(long long u) { adj[u].clear(); }
signed main() {
  long long v1, v2, e;
  cin >> v1 >> e;
  v2 = v1;
  BGraph g(v1, v2);
  vector<pair<long long, pair<long long, long long>>> vec;
  long long u, v, w;
  for (long long i = 0; i < e; ++i) {
    cin >> u >> v >> w;
    vec.push_back({w, {u, v}});
  }
  sort(vec.begin(), vec.end());
  long long st = 0, end = 1000000001, ans = 10000000000;
  while (st <= end) {
    long long mid = (st + end) / 2;
    for (long long i = 0; i < v1; i++) {
      g.deleteEdge(i);
    }
    for (long long i = 0; i < vec.size(); i++) {
      pair<long long, pair<long long, long long>> p = vec[i];
      if (p.first <= mid) {
        g.addEdge(p.second.first, p.second.second);
      } else {
        break;
      }
    }
    long long curr = g.hopcroftKarpAlgorithm();
    if (curr == v1) {
      ans = min(ans, mid);
      end = mid - 1;
    } else {
      st = mid + 1;
    }
  }
  if (ans == 10000000000) {
    cout << -1;
  } else {
    cout << ans;
  }
  return 0;
}
