#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
inline int read() {
  int x = 0;
  int f = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    f |= (ch == '-') ? 1 : 0;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 3) + (x << 1) + (ch ^ 48);
    ch = getchar();
  }
  return f ? -x : x;
}
const int INF = 1e9;
const int maxn = 2e5 + 10;
struct node {
  int to, nxt, cap, flow;
} edge[maxn << 1];
int head[maxn], tot = 0;
struct node1 {
  int u, v, w;
} pp[maxn];
inline void add(int u, int v, int w, int rw = 0) {
  edge[tot].to = v;
  edge[tot].nxt = head[u];
  edge[tot].cap = w;
  edge[tot].flow = 0;
  head[u] = tot++;
  edge[tot].to = u;
  edge[tot].nxt = head[v];
  edge[tot].cap = rw;
  edge[tot].flow = 0;
  head[v] = tot++;
}
int gap[maxn], dep[maxn];
int cur[maxn], pre[maxn];
int isap(int sta, int end, int N) {
  memset(gap, 0, sizeof(gap));
  memset(dep, 0, sizeof(dep));
  memcpy(cur, head, sizeof(head));
  int u = sta;
  pre[u] = -1;
  gap[0] = N;
  int ans = 0;
  while (dep[sta] < N) {
    if (u == end) {
      int Min = INF;
      for (int i = pre[u]; i != -1; i = pre[edge[i ^ 1].to])
        if (Min > edge[i].cap - edge[i].flow) Min = edge[i].cap - edge[i].flow;
      for (int i = pre[u]; i != -1; i = pre[edge[i ^ 1].to]) {
        edge[i].flow += Min;
        edge[i ^ 1].flow -= Min;
      }
      u = sta;
      ans += Min;
      continue;
    }
    int flag = 0;
    int v;
    for (int i = cur[u]; i != -1; i = edge[i].nxt) {
      v = edge[i].to;
      if (edge[i].cap - edge[i].flow && dep[v] + 1 == dep[u]) {
        flag = 1;
        cur[u] = pre[v] = i;
        break;
      }
    }
    if (flag) {
      u = v;
      continue;
    }
    int Min = N;
    for (int i = head[u]; i != -1; i = edge[i].nxt)
      if (edge[i].cap - edge[i].flow && dep[edge[i].to] < Min) {
        Min = dep[edge[i].to];
        cur[u] = i;
      }
    gap[dep[u]]--;
    if (!gap[dep[u]]) return ans;
    dep[u] = Min + 1;
    gap[dep[u]]++;
    if (u != sta) u = edge[pre[u] ^ 1].to;
  }
  return ans;
}
int n, m;
int main() {
  n = read();
  for (register int i = 1; i <= n * 2 + 4; ++i) head[i] = -1;
  m = read();
  int s = n * 2 + 1;
  int t = n * 2 + 2;
  for (register int i = 1; i <= m; ++i) {
    pp[i].u = read();
    pp[i].v = read();
    pp[i].w = read();
  }
  int l = 1, r = INF + 1;
  while (l < r) {
    int mid = (l + r) >> 1;
    for (register int i = 1; i <= t; ++i) head[i] = -1;
    tot = 0;
    for (register int i = 1; i <= m; ++i) {
      if (pp[i].w <= mid) add(pp[i].u, pp[i].v + n, 1);
    }
    for (register int i = 1; i <= n; ++i) add(s, i, 1), add(i + n, t, 1);
    int ans = isap(s, t, t);
    if (ans == n)
      r = mid;
    else
      l = mid + 1;
  }
  if (l != INF + 1)
    printf("%d\n", l);
  else
    printf("-1\n");
}
