#include <bits/stdc++.h>
using namespace std;
void IO() {
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
const long long int INF = (long long int)1e18;
const long long int C = (long long int)1e15;
struct Dinic {
  struct Edge {
    int v, to;
    long long int cap;
    Edge() : v(), to(), cap() {}
    Edge(int _v, int _to, long long int _cap) : v(_v), to(_to), cap(_cap) {}
  };
  vector<Edge> ed;
  vector<vector<int>> g;
  int second, T;
  int n;
  vector<int> dist;
  vector<int> idx;
  Dinic() : ed(), g(), second(), T() {}
  Dinic(int _n, int _S, int _T) {
    n = _n;
    second = _S;
    T = _T;
    g.resize(n);
    for (int i = 0; i < n; i++) g[i].clear();
    ed = vector<Edge>();
  }
  void addEdge(int v, int to, long long int cap) {
    g[v].push_back((int)ed.size());
    ed.push_back(Edge(v, to, cap));
    g[to].push_back((int)ed.size());
    ed.push_back(Edge(to, v, 0));
  }
  bool BFS() {
    dist.resize(n);
    vector<int> q;
    for (int i = 0; i < n; i++) dist[i] = n;
    q.push_back(second);
    dist[second] = 0;
    for (int i = 0; i < (int)q.size(); i++) {
      int v = q[i];
      for (int id : g[v]) {
        Edge e = ed[id];
        if (e.cap <= 0) continue;
        int u = e.to;
        if (dist[u] <= dist[v] + 1) continue;
        dist[u] = dist[v] + 1;
        q.push_back(u);
      }
    }
    return dist[T] < n;
  }
  long long int dfs(int v, long long int flow) {
    if (v == T || flow == 0) return flow;
    long long int res = 0;
    for (int &i = idx[v]; i < (int)g[v].size(); i++) {
      int id = g[v][i];
      Edge e = ed[id];
      int to = e.to;
      if (dist[to] != dist[v] + 1) continue;
      long long int df = dfs(to, min(flow, e.cap));
      flow -= df;
      res += df;
      ed[id].cap -= df;
      ed[id ^ 1].cap += df;
      if (flow == 0) return res;
    }
    return res;
  }
  long long int Flow() {
    idx.resize(n);
    long long int res = 0;
    while (BFS()) {
      for (int i = 0; i < n; i++) idx[i] = 0;
      res += dfs(second, INF);
    }
    return res;
  }
} G;
long long int n, m;
vector<pair<int, pair<int, int>>> edges;
int main() {
  IO();
  cin >> n >> m;
  edges.resize(m);
  for (__typeof(m) i = (0) - ((0) > (m)); i != (m) - ((0) > (m));
       i += 1 - 2 * ((0) > (m))) {
    cin >> edges[i].second.first >> edges[i].second.second >> edges[i].first;
    edges[i].second.first--;
    edges[i].second.second--;
  }
  sort(edges.begin(), edges.end());
  int l = 0, h = m - 1;
  int second = 2 * n, T = 2 * n + 1;
  int ans = -1;
  while (l <= h) {
    int mid = l + (h - l) / 2;
    G = Dinic(2 * n + 2, second, T);
    for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n));
         i += 1 - 2 * ((0) > (n))) {
      G.addEdge(second, i, 1);
      G.addEdge(n + i, T, 1);
    }
    for (__typeof(mid + 1) i = (0) - ((0) > (mid + 1));
         i != (mid + 1) - ((0) > (mid + 1)); i += 1 - 2 * ((0) > (mid + 1))) {
      G.addEdge(edges[i].second.first, n + edges[i].second.second, 1);
    }
    if (G.Flow() == n) {
      ans = mid;
      h = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  if (ans == -1)
    cout << -1;
  else
    cout << edges[ans].first;
  return 0;
}
