#include <bits/stdc++.h>
using namespace std;
int Read() {
  int val = 0, opt = 1;
  char ch;
  while (!isdigit(ch = getchar()))
    if (ch == '-') opt = -1;
  while (isdigit(ch)) (val *= 10) += ch - '0', ch = getchar();
  return val * opt;
}
struct Edge {
  int u, v, w;
} E[120100];
int n, m, Ans = INT_MAX, s, t;
int beg[20050], cur[20050], nxt[2 * 120100], gt[2 * 120100], cap[2 * 120100],
    tot = 1, dir[20050];
void add(int u, int v, int w) {
  nxt[++tot] = beg[u], beg[u] = tot, gt[tot] = v, cap[tot] = w;
}
queue<int> q;
int bfs() {
  int u, v;
  for (int i = 1; i <= t; i++) dir[i] = -1, cur[i] = beg[i];
  dir[s] = 0;
  while (!q.empty()) q.pop();
  q.push(s);
  while (!q.empty()) {
    u = q.front();
    q.pop();
    for (int e = beg[u]; e; e = nxt[e])
      if (cap[e] > 0 && dir[v = gt[e]] == -1) {
        dir[v] = dir[u] + 1;
        q.push(v);
        if (v == t) return 1;
      }
  }
  return 0;
}
int dinic(const int& u, const int& flow) {
  if (u == t) return flow;
  int res = 0, v, delta;
  for (int& e = cur[u]; e; e = nxt[e])
    if (cap[e] > 0 && dir[v = gt[e]] > dir[u]) {
      delta = dinic(v, min(flow - res, cap[e]));
      if (delta) {
        cap[e] -= delta;
        cap[e ^ 1] += delta;
        res += delta;
        if (res == flow) break;
      }
    }
  if (res != flow) dir[u] = -1;
  return res;
}
int check(const int& Max) {
  for (int i = 1; i <= 2 * n + 2; i++) beg[i] = 0;
  tot = 1;
  int u, v, w;
  for (int i = 1; i <= m; i++) {
    u = E[i].u, v = E[i].v, w = E[i].w;
    if (w > Max) continue;
    add(u, v + n, 1);
    add(v + n, u, 0);
  }
  s = 2 * n + 1, t = s + 1;
  for (int i = 1; i <= n; i++) {
    add(s, i, 1);
    add(i, s, 0);
    add(i + n, t, 1);
    add(t, i + n, 0);
  }
  int ans = 0;
  while (bfs()) ans += dinic(s, INT_MAX);
  return ans == n;
}
void work() {
  n = Read(), m = Read();
  int u, v, w;
  int x = 0, y = 0, mid;
  for (int i = 1; i <= m; i++) {
    u = Read(), v = Read(), w = Read();
    y = max(y, w);
    E[i] = {u, v, w};
  }
  while (x <= y) {
    mid = (x + y) >> 1;
    if (check(mid)) {
      Ans = min(Ans, mid);
      y = mid - 1;
    } else
      x = mid + 1;
  }
  printf("%d\n", Ans == INT_MAX ? -1 : Ans);
}
int main(void) {
  work();
  return 0;
}
