#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e5;
struct Edge {
  int v, flow, c, rev;
  Edge(int _v, int _flow, int _c, int _rev) {
    v = _v;
    flow = _flow;
    c = _c;
    rev = _rev;
  }
};
class Graph {
  int V, lvl[N + 5], start[N + 5];
  vector<Edge> adj[N + 5];

 public:
  void addEdge(int u, int v, int C) {
    Edge a(v, 0, C, adj[v].size());
    Edge b(u, 0, 0, adj[u].size());
    adj[u].push_back(a);
    adj[v].push_back(b);
  }
  bool BFS(int s, int t) {
    memset(lvl, -1, sizeof(lvl));
    lvl[s] = 0;
    queue<int> q;
    q.push(s);
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      for (auto e : adj[u])
        if (lvl[e.v] < 0 && e.flow < e.c) {
          lvl[e.v] = lvl[u] + 1;
          q.push(e.v);
        }
    }
    return lvl[t] != -1;
  }
  int DFS(int u, int flow, int t, int start[]) {
    if (u == t) return flow;
    for (; start[u] < adj[u].size(); start[u]++) {
      Edge& e = adj[u][start[u]];
      if (lvl[e.v] == lvl[u] + 1 && e.flow < e.c) {
        int curr = min(flow, e.c - e.flow);
        int tmp = DFS(e.v, curr, t, start);
        if (tmp > 0) {
          e.flow += tmp;
          adj[e.v][e.rev].flow -= tmp;
          return tmp;
        }
      }
    }
    return 0;
  }
  int dinic(int s, int t) {
    int total = 0;
    while (BFS(s, t)) {
      memset(start, 0, sizeof(start));
      while (int flow = DFS(s, INT_MAX, t, start)) total += flow;
    }
    return total;
  }
  int reset() {
    for (int i = 0; i < N + 3; i++) {
      adj[i].clear();
    }
    return 0;
  }
};
map<int, vector<pair<int, int> > > dat;
Graph ds;
int main() {
  dat.clear();
  int n, m;
  scanf("%d %d", &n, &m);
  const int src = 0;
  const int sink = 2 * n + 1;
  for (int i = 1; i <= m; i++) {
    int u, v, cost;
    scanf("%d %d %d", &u, &v, &cost);
    if (dat.find(cost) == dat.end()) dat[cost] = vector<pair<int, int> >();
    dat[cost].push_back(make_pair(u, v));
  }
  int res = -1;
  for (int lo = 0, hi = (int)1e9; lo <= hi;) {
    int mid = (lo + hi) >> 1;
    ds.reset();
    for (int i = 1; i <= n; i++) {
      ds.addEdge(src, i, 1);
      ds.addEdge(n + i, sink, 1);
    }
    for (auto& row : dat) {
      if (row.first > mid) break;
      for (pair<int, int> x : row.second) {
        ds.addEdge(x.first, n + x.second, 1);
      }
    }
    int ret = ds.dinic(src, sink);
    if (ret == n) {
      res = mid;
      hi = mid - 1;
    } else {
      lo = mid + 1;
    }
  }
  printf("%d\n", res);
  return 0;
}
