#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2e4 + 5;
long long ptr[maxn];
long long d[maxn];
long long n;
vector<pair<long long, long long> > v;
vector<vector<pair<long long, long long> > > a(maxn);
long long currflow;
pair<long long, long long> inv(pair<long long, long long> o) {
  swap(o.first, o.second);
  return o;
}
bool bfs() {
  for (long long i = 0; i < maxn; ++i) d[i] = 1e18;
  d[0] = 0;
  deque<long long> q;
  q.push_back(0);
  while (!q.empty()) {
    deque<long long> q1;
    while (!q.empty()) {
      long long curr = q.back();
      q.pop_back();
      for (auto z : a[curr]) {
        if (d[z.first] == 1e18 && v[z.second].first == curr) {
          d[z.first] = d[curr] + 1;
          q1.push_back(z.first);
        }
      }
    }
    q = q1;
  }
  return d[2 * n + 1] != 1e18;
}
bool dfs(long long x) {
  if (x == (2 * n + 1)) {
    ++currflow;
    return true;
  }
  for (long long i = ptr[x]; i < a[x].size(); ++i) {
    pair<long long, long long> h = a[x][i];
    if (v[h.second].first != x || d[h.first] != (d[x] + 1)) continue;
    long long z = h.first;
    if (dfs(z)) {
      v[h.second] = inv(v[h.second]);
      ptr[x] = (i + 1);
      return true;
    }
  }
  return false;
}
long long maxflow() {
  currflow = 0;
  while (bfs()) {
    for (long long i = 0; i < maxn; ++i) ptr[i] = 0;
    while (dfs(0)) {
    }
  }
  return currflow;
}
vector<pair<long long, pair<long long, long long> > > q;
bool check(long long k) {
  for (long long i = 0; i < k; ++i) {
    long long x = q[i].second.first;
    long long y = q[i].second.second;
    a[x].push_back({y + n, v.size()});
    a[y + n].push_back({x, v.size()});
    v.push_back({x, y + n});
  }
  return (maxflow() == n);
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long m;
  cin >> n >> m;
  for (long long i = 0; i < n; ++i) {
    a[0].push_back({i + 1, v.size()});
    a[i + 1].push_back({0, v.size()});
    v.push_back({0, i + 1});
  }
  for (long long i = 0; i < n; ++i) {
    a[2 * n + 1].push_back({i + n + 1, v.size()});
    a[i + n + 1].push_back({2 * n + 1, v.size()});
    v.push_back({i + n + 1, 2 * n + 1});
  }
  for (long long i = 0; i < m; ++i) {
    long long w, x, y;
    cin >> x >> y >> w;
    q.push_back({w, {x, y}});
  }
  vector<pair<long long, long long> > v1 = v;
  vector<vector<pair<long long, long long> > > a1 = a;
  sort(q.begin(), q.end());
  long long low = 0;
  long long up = (m + 1);
  while (true) {
    v = v1;
    a = a1;
    if ((up - low) == 1) {
      if (low == m) {
        cout << (-1);
        return 0;
      } else {
        cout << q[low].first;
        return 0;
      }
    }
    long long k = (low + up) / 2;
    if (check(k))
      up = k;
    else
      low = k;
  }
  return 0;
}
