#include <bits/stdc++.h>
using namespace std;
const long long mx = 105;
const long long mod = 1e9 + 7;
const long long maxn = 2e5 + 10;
long long dp[maxn], co[maxn];
vector<vector<long long>> adj;
vector<long long> a;
vector<long long> nc;
long long res;
struct FlowEdge {
  int v, u;
  long long cap, flow = 0;
  FlowEdge(int v, int u, long long cap) : v(v), u(u), cap(cap) {}
};
struct Dinic {
  const long long flow_inf = 1e18;
  vector<FlowEdge> edges;
  vector<vector<int>> adj;
  int n, m = 0;
  int s, t;
  vector<int> level, ptr;
  queue<int> q;
  Dinic(int n, int s, int t) : n(n), s(s), t(t) {
    adj.resize(n);
    level.resize(n);
    ptr.resize(n);
  }
  void add_edge(int v, int u, long long cap) {
    edges.emplace_back(v, u, cap);
    edges.emplace_back(u, v, 0);
    adj[v].push_back(m);
    adj[u].push_back(m + 1);
    m += 2;
  }
  bool bfs() {
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      for (int id : adj[v]) {
        if (edges[id].cap - edges[id].flow < 1) continue;
        if (level[edges[id].u] != -1) continue;
        level[edges[id].u] = level[v] + 1;
        q.push(edges[id].u);
      }
    }
    return level[t] != -1;
  }
  long long dfs(int v, long long pushed) {
    if (pushed == 0) return 0;
    if (v == t) return pushed;
    for (int& cid = ptr[v]; cid < (int)adj[v].size(); cid++) {
      int id = adj[v][cid];
      int u = edges[id].u;
      if (level[v] + 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;
  }
};
void solve() {
  long long n, i, j, k, m;
  cin >> n >> m;
  vector<pair<pair<long long, long long>, long long>> v;
  for (i = 0; i < m; i++) {
    long long a, b, c;
    cin >> a >> b >> c;
    v.push_back({{a, b}, c});
  }
  long long l = 1, h = 1e9 + 1, mi, res = -1;
  while (l <= h) {
    mi = l + (h - l) / 2;
    Dinic s(2 * n + 3, 2 * n + 1, 2 * n + 2);
    for (i = 0; i < m; i++) {
      if (v[i].second <= mi) {
        s.add_edge(v[i].first.first, n + v[i].first.second, 1);
      }
    }
    for (i = 1; i <= n; i++) {
      s.add_edge(2 * n + 1, i, 1);
      s.add_edge(n + i, 2 * n + 2, 1);
    }
    long long te = s.flow();
    if (te == n) {
      h = mi - 1;
      res = mi;
    } else {
      l = mi + 1;
    }
  }
  cout << res << "\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long t = 1, i, j, k;
  while (t--) {
    solve();
  }
}
