#include <bits/stdc++.h>
using namespace std;
const long long int M = 1e9 + 7;
long long int inf = 1e18;
long long int begtime = clock();
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
long long int n, m, k;
long long int binpow(long long int val, long long int deg) {
  if (deg < 0) return 0;
  if (!deg) return 1 % M;
  if (deg & 1) return binpow(val, deg - 1) * val % M;
  long long int res = binpow(val, deg >> 1);
  return (res * res) % M;
}
long long int modinv(long long int n) { return binpow(n, M - 2); }
long long int gcd(long long int a, long long int b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
struct FlowEdge {
  long long int vector, u;
  long long cap, flow = 0;
  FlowEdge(long long int vector, long long int u, long long cap)
      : vector(vector), u(u), cap(cap) {}
};
struct Dinic {
  const long long flow_inf = 1e18;
  vector<FlowEdge> edges;
  vector<vector<long long int>> adj;
  long long int n, m = 0;
  long long int s, t;
  vector<long long int> level, ptr;
  queue<long long int> q;
  Dinic(long long int n, long long int s, long long int t) : n(n), s(s), t(t) {
    adj.resize(n);
    level.resize(n);
    ptr.resize(n);
  }
  void add_edge(long long int vector, long long int u, long long cap) {
    edges.emplace_back(vector, u, cap);
    edges.emplace_back(u, vector, 0);
    adj[vector].push_back(m);
    adj[u].push_back(m + 1);
    m += 2;
  }
  bool bfs() {
    while (!q.empty()) {
      long long int vector = q.front();
      q.pop();
      for (long long int id : adj[vector]) {
        if (edges[id].cap - edges[id].flow < 1) continue;
        if (level[edges[id].u] != -1) continue;
        level[edges[id].u] = level[vector] + 1;
        q.push(edges[id].u);
      }
    }
    return level[t] != -1;
  }
  long long dfs(long long int vector, long long pushed) {
    if (pushed == 0) return 0;
    if (vector == t) return pushed;
    for (long long int& cid = ptr[vector];
         cid < (long long int)adj[vector].size(); cid++) {
      long long int id = adj[vector][cid];
      long long int u = edges[id].u;
      if (level[vector] + 1 != level[u] || edges[id].cap - edges[id].flow < 1)
        continue;
      long long tr = dfs(u, min(pushed, edges[id].cap - edges[id].flow));
      if (tr == 0) continue;
      edges[id].flow += tr;
      edges[id ^ 1].flow -= tr;
      return tr;
    }
    return 0;
  }
  long long flow() {
    long long f = 0;
    while (true) {
      fill(level.begin(), level.end(), -1);
      level[s] = 0;
      q.push(s);
      if (!bfs()) break;
      fill(ptr.begin(), ptr.end(), 0);
      while (long long pushed = dfs(s, flow_inf)) {
        f += pushed;
      }
    }
    return f;
  }
};
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long int i, j, t, x, y, z;
  cin >> n >> m;
  vector<pair<long long int, pair<long long int, long long int>>> road;
  for (i = 0; i < m; i++) {
    cin >> x >> y >> z;
    road.push_back({z, {x, y}});
  }
  sort(road.begin(), road.end());
  long long int l = 1, r = m, mid, ans = -1;
  while (l <= r) {
    mid = (l + r) / 2;
    Dinic f(2 * n + 2, 0, 2 * n + 1);
    for (i = 0; i < mid; i++) {
      f.add_edge(road[i].second.first, road[i].second.second + n, 1);
    }
    for (i = 0; i < n; i++) {
      f.add_edge(0, i + 1, 1);
      f.add_edge(i + 1 + n, 2 * n + 1, 1);
    }
    x = f.flow();
    if (x < n)
      l = mid + 1;
    else {
      ans = road[mid - 1].first;
      r = mid - 1;
    }
  }
  cout << ans;
  return 0;
}
