#include <bits/stdc++.h>
using namespace std;
using LL = long long;
constexpr int N = 1e4 + 5;
vector<int> E[N];
int seen[N];
int matched[N];
int used;
bool dfs(int at) {
  if (seen[at] == used) return false;
  seen[at] = used;
  for (const int& to : E[at]) {
    if (matched[to] == -1 || dfs(matched[to])) {
      matched[to] = at;
      return true;
    }
  }
  return false;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  vector<tuple<int, int, int>> in(m);
  for (int i = 0; i < m; i++) {
    int u, v, d;
    cin >> u >> v >> d;
    in[i] = {d, u, v};
  }
  sort(begin(in), end(in));
  int l = 0, r = m - 1;
  int res = -1;
  while (l <= r) {
    int mid = (l + r) / 2;
    for (int i = 1; i <= n; i++) E[i].clear();
    for (int i = 0; i <= mid; i++) E[get<1>(in[i])].push_back(get<2>(in[i]));
    bool ok = true;
    for (int i = 1; i <= n; i++) matched[i] = -1;
    for (int i = 1; i <= n; i++) {
      used++;
      if (!dfs(i)) {
        ok = false;
        break;
      }
    }
    if (ok) {
      res = get<0>(in[mid]);
      r = mid - 1;
    } else
      l = mid + 1;
  }
  cout << res << "\n";
}
