#include <bits/stdc++.h>
using namespace std;
struct matching {
  vector<vector<int>> edge;
  vector<int> pa;
  vector<int> pb;
  vector<int> was;
  int n, m;
  int res;
  int iter;
  matching(int n, int m) : n(n), m(m) {
    edge.resize(n);
    pa.resize(n, -1);
    pb.resize(m, -1);
    was.resize(n, 0);
    iter = 0;
    res = 0;
  }
  void add(int from, int to) {
    assert(0 <= from && from < n && to >= 0 && to < m);
    edge[from].emplace_back(to);
  }
  bool dfs(int v) {
    was[v] = iter;
    for (auto& u : edge[v]) {
      if (pb[u] == -1) {
        pa[v] = u;
        pb[u] = v;
        return true;
      }
    }
    for (auto& u : edge[v]) {
      if (was[pb[u]] != iter && dfs(pb[u])) {
        pb[u] = v;
        pa[v] = u;
        return true;
      }
    }
    return false;
  }
  int solve() {
    while (1) {
      ++iter;
      int add = 0;
      for (int i = 0; i < n; i++) {
        if (pa[i] == -1 && dfs(i)) {
          add++;
        }
      }
      if (!add) break;
      res += add;
    }
    return res;
  }
};
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<tuple<int, int, int>> edges;
  for (int i = 0; i < m; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    --u;
    --v;
    edges.emplace_back(w, u, v);
  }
  sort(edges.begin(), edges.end());
  auto test = [&](int val) {
    matching mat(n, n);
    for (auto& x : edges) {
      if (get<0>(x) > val) break;
      mat.add(get<1>(x), get<2>(x));
    }
    return (mat.solve() == n);
  };
  int l = 0, r = m - 1;
  while (l <= r) {
    int mid = l + r >> 1;
    if (test(get<0>(edges[mid])))
      r = mid - 1;
    else
      l = mid + 1;
  }
  if (l == m)
    cout << "-1";
  else
    cout << get<0>(edges[l]);
}
