#include <bits/stdc++.h>
using namespace std;
const long long SZ = 2e4 + 4;
const long long inf = 2e9;
long long n, m;
vector<pair<pair<long long, long long>, long long>> e;
void read() {
  cin >> n >> m;
  for (long long i = 0; i < m; ++i) {
    long long u, v, w;
    cin >> u >> v >> w;
    --u, --v;
    e.emplace_back(make_pair(u, v), w);
  }
}
long long s, t;
vector<vector<pair<long long, long long>>> g;
vector<long long> c;
vector<long long> f;
vector<long long> cost;
long long add_vertex() {
  g.emplace_back();
  return g.size() - 1;
}
void add_edge(long long u, long long v, long long w, long long z) {
  g[u].emplace_back(v, c.size());
  c.push_back(w);
  f.push_back(0);
  cost.push_back(z);
  g[v].emplace_back(u, c.size());
  c.push_back(0);
  f.push_back(0);
  cost.push_back(z);
}
long long d[SZ];
bool bfs(long long mx) {
  memset(d, 1, sizeof(d));
  long long inf = d[0];
  d[s] = 0;
  queue<long long> q;
  q.push(s);
  while (!q.empty()) {
    long long u = q.front();
    q.pop();
    for (auto [v, i] : g[u]) {
      if (f[i] < c[i] && cost[i] <= mx && d[v] == inf) {
        d[v] = d[u] + 1;
        q.push(v);
      }
    }
  }
  return d[t] != inf;
}
long long p[SZ];
long long dfs(long long u, long long mx, long long flow = inf) {
  if (u == t || flow == 0) return flow;
  for (long long &j = p[u]; j < g[u].size(); ++j) {
    auto [v, i] = g[u][j];
    if (d[v] == d[u] + 1 && cost[i] <= mx) {
      long long delta = dfs(v, mx, min(flow, c[i] - f[i]));
      if (delta) {
        f[i] += delta;
        f[i ^ 1] -= delta;
        return delta;
      }
    }
  }
  return 0;
}
bool check(long long mx) {
  fill(f.begin(), f.end(), 0);
  long long ans = 0;
  while (bfs(mx)) {
    memset(p, 0, sizeof(p));
    long long flow = dfs(s, mx);
    while (flow) {
      ans += flow;
      flow = dfs(s, mx);
    }
  }
  return ans == n;
}
void solve() {
  s = add_vertex();
  t = add_vertex();
  vector<long long> a, b;
  for (long long i = 0; i < n; ++i) {
    long long u = add_vertex();
    a.push_back(u);
    add_edge(s, u, 1, 0);
  }
  for (long long i = 0; i < n; ++i) {
    long long v = add_vertex();
    b.push_back(v);
    add_edge(v, t, 1, 0);
  }
  for (long long i = 0; i < m; ++i) {
    add_edge(a[e[i].first.first], b[e[i].first.second], 1, e[i].second);
  }
  long long lo = -1, hi = 2e9;
  while (hi - lo > 1) {
    long long m = lo + ((hi - lo) >> 1);
    if (check(m)) {
      hi = m;
    } else {
      lo = m;
    }
  }
  if (hi == 2e9) {
    cout << -1 << "\n";
  } else {
    cout << hi << "\n";
  }
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  read();
  solve();
  return 0;
}
