#include <bits/stdc++.h>
using namespace std;
const long long M = 1e9 + 7;
const long long mod = 998244353;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
long long binpow(long long a, long long b) {
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = res * a % M;
    a = a * a % M;
    b >>= 1;
  }
  return res;
}
int l, r, mid;
struct Kuhn {
  vector<vector<pair<int, int>>> adj;
  vector<int> matchA, matchB, marcB;
  int n, m;
  Kuhn(int n, int m) : n(n), m(m) {
    adj.resize(n, vector<pair<int, int>>());
    matchA.resize(n);
    matchB = marcB = vector<int>(m);
  }
  void add_edge(int u, int v, int c) { adj[u].emplace_back(v, c); }
  bool dfs(int u) {
    for (pair<int, int> &p : adj[u]) {
      int v = p.first;
      if (marcB[v] or p.second > mid) continue;
      marcB[v] = 1;
      if (matchB[v] == -1 or dfs(matchB[v])) {
        matchB[v] = u;
        matchA[u] = v;
        return true;
      }
    }
    return false;
  }
  int matching() {
    for (int i = 0; i < n; i++) matchA[i] = -1;
    for (int i = 0; i < m; i++) matchB[i] = -1;
    shuffle(adj.begin(), adj.end(), rng);
    for (auto v : adj) shuffle(v.begin(), v.end(), rng);
    int res = 0;
    bool aux = true;
    while (aux) {
      for (int j = 0; j < m; j++) marcB[j] = 0;
      aux = false;
      for (int i = 0; i < n; i++) {
        if (matchA[i] != -1) continue;
        if (dfs(i)) {
          res++;
          aux = true;
        }
      }
    }
    return res;
  }
};
void solve() {
  int n, m;
  cin >> n >> m;
  Kuhn G(n, n);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    G.add_edge(a, b, c);
  }
  l = 0, r = 1e9 + 1;
  int ans = -1;
  while (l <= r) {
    mid = l + r >> 1;
    if (G.matching() == n) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  cout << ans << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
}
