#include <bits/stdc++.h>
using namespace std;
struct edge {
  long long int to, from, flow, capacity, id;
};
long long int n, m, source, destiny;
vector<edge> adj[100001];
vector<pair<pair<long long int, long long int>, long long int> > ar;
queue<long long int> q;
long long int level[100001];
long long int v[100001];
long long int ptr[100001];
void add_edge(long long int a, long long int b, long long int c) {
  adj[a].push_back({b, (long long int)adj[b].size(), c, c, 0});
  adj[b].push_back({a, (long long int)adj[a].size() - 1, 0, 0, 0});
}
bool bfs() {
  memset(level, -1, sizeof(level));
  level[source] = 0;
  q.push(source);
  while (!q.empty()) {
    long long int u = q.front();
    q.pop();
    for (auto at : adj[u]) {
      if (at.flow && level[at.to] == -1) {
        q.push(at.to);
        level[at.to] = level[u] + 1;
      }
    }
  }
  return level[destiny] != -1;
}
long long int dfs(long long int u, long long int flow) {
  if (u == destiny || flow == 0) return flow;
  for (long long int &p = ptr[u]; p < adj[u].size(); p++) {
    edge &at = adj[u][p];
    if (at.flow && level[u] == level[at.to] - 1) {
      long long int kappa = dfs(at.to, min(flow, at.flow));
      at.flow -= kappa;
      adj[at.to][at.from].flow += kappa;
      if (kappa != 0) return kappa;
    }
  }
  return 0;
}
long long int dinic() {
  long long int max_flow = 0;
  while (bfs()) {
    memset(ptr, 0, sizeof(ptr));
    while (1) {
      long long int flow = dfs(source, 1e18);
      if (flow == 0) break;
      max_flow += flow;
    }
  }
  return max_flow;
}
bool ok(long long int cc) {
  long long int cnt = 0;
  for (long long int i = 0; i <= n + n + 1; i++) adj[i].clear();
  for (long long int i = 1; i <= n; i++) add_edge(source, i, 1);
  for (long long int i = n + 1; i <= n + n; i++) add_edge(i, destiny, 1);
  for (auto const &i : ar) {
    if (cnt >= cc) break;
    add_edge(i.first.first, i.first.second, 1);
    cnt++;
  }
  return (dinic() == n);
}
bool cmp(pair<pair<long long int, long long int>, long long int> a,
         pair<pair<long long int, long long int>, long long int> b) {
  return a.second < b.second;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  scanf("%lld%lld", &n, &m);
  source = 0, destiny = n + n + 1;
  for (long long int i = 0; i < m; i++) {
    long long int a, b, c;
    scanf("%lld%lld%lld", &a, &b, &c);
    ar.push_back({{a, b + n}, c});
  }
  sort(ar.begin(), ar.end(), cmp);
  long long int i = 0, f = ar.size() + 1, mid;
  while (i < f) {
    mid = (i + f) / 2;
    (ok(mid)) ? f = mid : i = mid + 1;
  }
  printf("%lld\n", (i == ar.size() + 1) ? -1 : ar[i - 1].second);
  return 0;
}
