#include <bits/stdc++.h>
using namespace std;
const long N = 10005;
long n, m, dd[N], match[N], ti, kq, l, r, mid;
vector<long> ke[N], cp[N], c;
bool dfs(long u) {
  for (long i = 0; i < ke[u].size(); i++)
    if (cp[u][i] <= mid) {
      long v = ke[u][i];
      if (dd[v] == ti) continue;
      dd[v] = ti;
      if (match[v] == 0) {
        match[v] = u;
        return 1;
      }
      if (dfs(match[v])) {
        match[v] = u;
        return 1;
      }
    }
  return 0;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (long i = 1; i <= m; i++) {
    long u, v, w;
    cin >> u >> v >> w;
    ke[u].push_back(v);
    cp[u].push_back(w);
  }
  l = 1;
  r = 1000000001;
  while (l < r) {
    mid = (l + r) >> 1;
    c.clear();
    for (long i = 1; i <= n; i++) {
      dd[i] = match[i] = 0;
      c.push_back(i);
    }
    ti = 0;
    kq = 0;
    while (1) {
      ti++;
      long cu = c.size();
      for (long i = c.size() - 1; i >= 0; i--)
        if (dfs(c[i])) {
          kq++;
          c[i] = c.back();
          c.pop_back();
        }
      if (cu == c.size()) break;
    }
    if (kq == n)
      r = mid;
    else
      l = mid + 1;
  }
  if (l == 1000000001)
    cout << -1;
  else
    cout << l;
  return 0;
}
