#include <bits/stdc++.h>
using namespace std;
const long long inf = 0x3f3f3f3f3f3f3f3f;
class BPGraph {
 public:
  long long n;
  long long m;
  vector<long long> *adj;
  long long *pair_left;
  long long *pair_right;
  long long *dis;
  BPGraph(long long n, long long m) {
    this->n = n;
    this->m = m;
    adj = new vector<long long>[n + 1];
  }
  void add_Edge(long long u, long long v) { adj[u].push_back(v); }
  bool bfs() {
    queue<long long> q;
    dis[0] = inf;
    for (long long i = 1; i <= n; i++) {
      if (pair_left[i] == 0) {
        dis[i] = 0;
        q.push(i);
      } else {
        dis[i] = inf;
      }
    }
    while (!q.empty()) {
      long long u = q.front();
      q.pop();
      if (dis[u] < dis[0]) {
        for (long long v : adj[u]) {
          long long pair = pair_right[v];
          if (dis[pair] == inf) {
            dis[pair] = dis[u] + 1;
            q.push(pair);
          }
        }
      }
    }
    return (dis[0] < inf);
  }
  bool dfs(long long u) {
    if (!u) return true;
    for (long long v : adj[u]) {
      long long pair = pair_right[v];
      if (dis[pair] == dis[u] + 1) {
        if (dfs(pair)) {
          pair_left[u] = v;
          pair_right[v] = u;
          return true;
        }
      }
    }
    return false;
  }
  long long hopcroft_karp() {
    pair_left = new long long[n + 1];
    pair_right = new long long[m + 1];
    dis = new long long[n + 1];
    for (long long i = 0; i <= n; i++) {
      pair_left[i] = 0;
    }
    for (long long i = 0; i <= m; i++) {
      pair_right[i] = 0;
    }
    long long ans = 0;
    while (bfs()) {
      for (long long u = 1; u <= n; u++) {
        if (!pair_left[u] && dfs(u)) {
          ans++;
        }
      }
    }
    return ans;
  }
};
int32_t main() {
  long long n, m;
  cin >> n >> m;
  vector<pair<long long, pair<long long, long long>>> edge;
  for (long long i = 0; i < m; i++) {
    long long a, b;
    cin >> a >> b;
    long long t;
    cin >> t;
    edge.push_back({t, {a, b}});
  }
  sort(edge.begin(), edge.end());
  long long low = 0, high = inf, ans = inf;
  while (low <= high) {
    long long mid = low + (high - low) / 2;
    BPGraph g(n, n);
    for (long long i = 0; i < m; i++) {
      if (edge[i].first > mid) break;
      g.add_Edge(edge[i].second.first, edge[i].second.second);
    }
    if (g.hopcroft_karp() == n) {
      ans = min(ans, mid);
      high = mid - 1;
    } else {
      low = mid + 1;
    }
  }
  if (ans < inf)
    cout << ans << endl;
  else
    cout << -1 << endl;
}
