#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 7;
int n, m;
class Edge {
 public:
  int u, v, w;
  inline bool operator<(const Edge &oth) const { return w < oth.w; }
} ed[100005];
namespace Dinic {
static const int maxn = 2e4 + 5;
static const int maxm = 3e5 + 5;
int tot, hd, tl;
int head[maxn], level[maxn], iter[maxn], que[maxn];
int to[maxm], cap[maxm], nxt[maxm];
inline void init() {
  tot = 0;
  memset(head, -1, sizeof(head));
  return;
}
inline void add(int u, int v, int w) {
  to[tot] = v;
  cap[tot] = w;
  nxt[tot] = head[u];
  head[u] = tot++;
  return;
}
inline void addEdge(int u, int v, int w) {
  add(u, v, w);
  add(v, u, 0);
  return;
}
inline void bfs(int s) {
  memset(level, -1, sizeof(level));
  hd = tl = 0;
  que[tl++] = s;
  level[s] = 0;
  while (hd < tl) {
    int u = que[hd++];
    for (int i = head[u]; ~i; i = nxt[i]) {
      int v = to[i];
      if (cap[i] && !~level[v]) {
        level[v] = level[u] + 1;
        que[tl++] = v;
      }
    }
  }
  return;
}
inline int dfs(int u, int t, int f) {
  if (u == t) return f;
  for (int &i = iter[u]; ~i; i = nxt[i]) {
    int v = to[i];
    if (cap[i] && level[v] > level[u]) {
      int d = dfs(v, t, min(cap[i], f));
      if (d) {
        cap[i] -= d;
        cap[i ^ 1] += d;
        return d;
      }
    }
  }
  return 0;
}
inline int maxFlow(int s, int t) {
  int ret = 0;
  while (true) {
    for (int u = (0); u < (maxn); ++u) iter[u] = head[u];
    bfs(s);
    if (!~level[t]) break;
    for (int f; f = dfs(s, t, INF); ret += f)
      ;
  }
  return ret;
}
}  // namespace Dinic
inline bool check(int x) {
  Dinic::init();
  for (int i = (0); i <= (x); ++i) {
    Dinic::addEdge(ed[i].u, ed[i].v + n, 1);
  }
  for (int i = (0); i < (n); ++i) Dinic::addEdge(n << 1, i, 1);
  for (int i = (0); i < (n); ++i) Dinic::addEdge(i + n, n << 1 | 1, 1);
  return Dinic::maxFlow(n << 1, n << 1 | 1) == n;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = (0); i < (m); ++i) {
    scanf("%d%d%d", &ed[i].u, &ed[i].v, &ed[i].w);
    --ed[i].u, --ed[i].v;
  }
  sort(ed, ed + m);
  int lb = -1, rb = m;
  for (; lb + 1 < rb;) {
    int md = lb + rb >> 1;
    if (check(md))
      rb = md;
    else
      lb = md;
  }
  if (rb == m)
    puts("-1");
  else
    printf("%d\n", ed[rb].w);
  return 0;
}
