#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using namespace std;
const int inf = 1e9 + 5;
const long long int inf64 = 1e18 + 5;
using ppi = pair<int, pair<int, int>>;
struct BipartiteMatcher {
  vector<vector<int>> G;
  vector<int> L, R, Viz;
  BipartiteMatcher(int n, int m) : G(n), L(n, -1), R(m, -1), Viz(n) {}
  void AddEdge(int a, int b) { G[a].push_back(b); }
  bool Match(int node) {
    if (Viz[node]) return false;
    Viz[node] = true;
    for (auto vec : G[node]) {
      if (R[vec] == -1) {
        L[node] = vec;
        R[vec] = node;
        return true;
      }
    }
    for (auto vec : G[node]) {
      if (Match(R[vec])) {
        L[node] = vec;
        R[vec] = node;
        return true;
      }
    }
    return false;
  }
  int Solve() {
    int ok = true;
    while (ok--) {
      fill(Viz.begin(), Viz.end(), 0);
      for (int i = 0; i < (int)L.size(); ++i)
        if (L[i] == -1) ok |= Match(i);
    }
    int ret = 0;
    for (int i = 0; i < L.size(); ++i) ret += (L[i] != -1);
    return ret;
  }
};
bool chk(int n, vector<ppi> &ed, int mid) {
  BipartiteMatcher BM(n, n);
  for (ppi p : ed) {
    if (p.first > mid) break;
    BM.AddEdge(p.second.first, p.second.second);
  }
  return (BM.Solve() == n);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m, i;
  cin >> n >> m;
  vector<ppi> ed;
  for (i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    --a, --b;
    ed.emplace_back(c, make_pair(a, b));
  }
  sort(ed.begin(), ed.end());
  int first = 0, l = ed.size() - 1, mid, ans = inf;
  while (first <= l) {
    mid = (first + l) / 2;
    if (chk(n, ed, ed[mid].first)) {
      ans = min(ans, ed[mid].first);
      l = mid - 1;
    } else
      first = mid + 1;
  }
  cout << (ans == inf ? -1 : ans);
}
