#include <bits/stdc++.h>
using namespace std;
int read() {
  int X = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') w = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') X = X * 10 + c - '0', c = getchar();
  return X * w;
}
const int N = 10000 + 10, M = 100000 + 10;
struct edge {
  int v, w, nxt;
} e[(N + N + M) << 1];
int head[N << 1], ecnt;
void addEdge(int u, int v, int w) {
  e[++ecnt] = (edge){v, w, head[u]}, head[u] = ecnt;
  e[++ecnt] = (edge){u, 0, head[v]}, head[v] = ecnt;
}
int S, T, lv[N << 1];
bool bfs() {
  memset(lv, 0, sizeof(lv)), lv[S] = 1;
  queue<int> Q;
  Q.push(S);
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    for (int i = head[u]; i; i = e[i].nxt) {
      int v = e[i].v, w = e[i].w;
      if (w && !lv[v]) lv[v] = lv[u] + 1, Q.push(v);
    }
  }
  return lv[T] != 0;
}
int dfs(int u, int r) {
  if (u == T || !r) return r;
  int add = 0;
  for (int i = head[u]; i; i = e[i].nxt) {
    int v = e[i].v, w = e[i].w;
    if (w && lv[v] == lv[u] + 1) {
      int t = dfs(v, min(r, w));
      e[i].w -= t, e[i ^ 1].w += t, add += t, r -= t;
      if (!r) break;
    }
  }
  if (!add) lv[u] = 0;
  return add;
}
int dinic() {
  int s = 0;
  while (bfs()) s += dfs(S, 2e9);
  return s;
}
int n, m;
struct sedge {
  int u, v, w;
} se[M];
int sta[M], top = 0;
bool check(int mid) {
  memset(head, 0, sizeof(head)), ecnt = 1;
  S = 0, T = n << 1 | 1;
  for (int i = 1; i <= m; ++i)
    if (se[i].w <= mid) addEdge(se[i].u, se[i].v + n, 1);
  for (int i = 1; i <= n; ++i) addEdge(S, i, 1);
  for (int i = 1; i <= n; ++i) addEdge(i + n, T, 1);
  return dinic() == n;
}
int main() {
  n = read(), m = read();
  for (int i = 1; i <= m; ++i) {
    se[i] = (sedge){read(), read(), read()};
    sta[++top] = se[i].w;
  }
  sort(sta + 1, sta + top + 1);
  top = unique(sta + 1, sta + top + 1) - sta - 1;
  if (!check(sta[top])) {
    puts("-1");
    return 0;
  }
  int L = 1, R = top;
  while (L < R) {
    int mid = (L + R) >> 1;
    if (check(sta[mid]))
      R = mid;
    else
      L = mid + 1;
  }
  printf("%d\n", sta[L]);
  return 0;
}
