#include <bits/stdc++.h>
using namespace std;
class matching {
 public:
  vector<vector<int>> g;
  vector<vector<int>> g_rev;
  vector<int> pa;
  vector<int> pb;
  vector<int> was_a;
  vector<int> was_b;
  int n, m;
  int res;
  int iter;
  matching(int _n, int _m) : n(_n), m(_m) {
    assert(0 <= n && 0 <= m);
    pa = vector<int>(n, -1);
    pb = vector<int>(m, -1);
    was_a = vector<int>(n, 0);
    was_b = vector<int>(m, 0);
    g.resize(n);
    g_rev.resize(m);
    res = 0;
    iter = 0;
  }
  void add(int from, int to) {
    assert(0 <= from && from < n && 0 <= to && to < m);
    g[from].push_back(to);
    g_rev[to].push_back(from);
  }
  bool dfs(int v) {
    was_a[v] = iter;
    for (int u : g[v]) {
      if (pb[u] == -1) {
        pa[v] = u;
        pb[u] = v;
        return true;
      }
    }
    for (int u : g[v]) {
      if (was_a[pb[u]] != iter && dfs(pb[u])) {
        pa[v] = u;
        pb[u] = v;
        return true;
      }
    }
    return false;
  }
  bool dfs_rev(int v) {
    was_b[v] = iter;
    for (int u : g_rev[v]) {
      if (pa[u] == -1) {
        pb[v] = u;
        pa[u] = v;
        return true;
      }
    }
    for (int u : g_rev[v]) {
      if (was_b[pa[u]] != iter && dfs_rev(pa[u])) {
        pb[v] = u;
        pa[u] = v;
        return true;
      }
    }
    return false;
  }
  int solve() {
    while (true) {
      int add = 0;
      iter++;
      for (int i = 0; i < n; i++) {
        if (pa[i] == -1 && dfs(i)) {
          add++;
        }
      }
      for (int i = 0; i < m; i++) {
        if (pb[i] == -1 && dfs_rev(i)) {
          add++;
        }
      }
      if (add == 0) {
        break;
      }
      res += add;
    }
    return res;
  }
  int run_one(int v) {
    if (pa[v] != -1) {
      return 0;
    }
    iter++;
    return (int)dfs(v);
  }
};
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]);
}
