#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using vi = vector<int>;
using vii = vector<pair<int, int>>;
constexpr int mod = 1e9 + 7, N = 2e5 + 5;
constexpr long long inf = 1e18;
constexpr long double eps = 1e-6;
struct Edge {
  int on, to, cap, flow;
};
struct Dinic {
  vector<vector<int>> adj;
  vector<Edge> Edges;
  vector<int> ptr, level;
  int s, t;
  int n, m = 0;
  Dinic(int n, int s, int t) : n(n), s(s), t(t) {
    adj.resize(n, vector<int>());
    level.resize(n);
    ptr.resize(n);
  }
  void addEdge(int u, int v, int cap) {
    Edges.push_back({u, v, cap, 0});
    Edges.push_back({v, u, 0, 0});
    adj[u].push_back(m);
    adj[v].push_back(m + 1);
    m += 2;
  }
  int maxFlow() {
    int flow = 0, cur = 0;
    while (bfs()) {
      fill(ptr.begin(), ptr.end(), 0);
      while (cur = dfs(s, 1e9)) flow += cur;
    }
    return flow;
  }
  int bfs() {
    fill(level.begin(), level.end(), -1);
    queue<int> q;
    level[s] = 0;
    q.push(s);
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      for (auto &x : adj[v]) {
        int to = Edges[x].to;
        if (level[to] == -1 && Edges[x].cap - Edges[x].flow > 0) {
          q.push(to);
          level[to] = level[v] + 1;
        }
      }
    }
    return level[t] != -1;
  }
  int dfs(int v, int pushed) {
    if (pushed == 0) return 0;
    if (v == t) return pushed;
    for (; ptr[v] < adj[v].size(); ptr[v]++) {
      int e = adj[v][ptr[v]];
      int to = Edges[e].to;
      if (level[to] == level[v] + 1 && Edges[e].cap - Edges[e].flow > 0) {
        int cur = dfs(to, min(pushed, Edges[e].cap - Edges[e].flow));
        if (cur != 0) {
          Edges[e].flow += cur;
          Edges[e ^ 1].flow -= cur;
          return cur;
        }
      }
    }
    return 0;
  }
};
void Onigiri() {
  int n, m;
  cin >> n >> m;
  struct query {
    int x, y, z;
  };
  vector<query> v;
  for (int i = 0; i < m; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    v.push_back({x, n + y, z});
  }
  for (int i = 1; i < n + 1; i++) {
    v.push_back({0, i, 1});
    v.push_back({i + n, 2 * n + 1, 1});
  }
  int l = 1, r = 1e9, ans = -1;
  while (l <= r) {
    int mid = (l + r) / 2;
    Dinic solver(2 * n + 2, 0, 2 * n + 1);
    for (auto &e : v) {
      if (e.z <= mid) solver.addEdge(e.x, e.y, 1);
    }
    if (solver.maxFlow() == n) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  while (t--) {
    Onigiri();
    cout << "\n";
  }
  cerr << "\n" << (float)clock() / CLOCKS_PER_SEC * 1000 << " ms" << endl;
  return 0;
}
