#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 5;
const long long maxn = 1e6 + 5;
const long long inf = 0x3f3f3f3f;
long long m, n, t, cnt, cur[maxn], dep[maxn], head[maxn];
struct node {
  long long v, val, next;
} a[maxn];
struct edge {
  long long u, v, w;
} b[maxn];
void add(long long u, long long v, long long val) {
  a[cnt].v = v;
  a[cnt].val = val;
  a[cnt].next = head[u];
  head[u] = cnt++;
  a[cnt].v = u;
  a[cnt].val = 0;
  a[cnt].next = head[v];
  head[v] = cnt++;
}
bool bfs(long long s) {
  queue<long long> que;
  memset(dep, 0, sizeof dep);
  for (long long i = 0; i <= 2 * n; i++) cur[i] = head[i];
  long long u = s;
  dep[u] = 1;
  que.push(u);
  while (!que.empty()) {
    long long u = que.front();
    que.pop();
    for (long long i = head[u]; ~i; i = a[i].next) {
      long long v = a[i].v;
      long long val = a[i].val;
      if (!dep[v] && val) {
        dep[v] = dep[u] + 1;
        que.push(v);
      }
    }
    if (dep[t]) return true;
  }
  return false;
}
long long dfs(long long u, long long uv) {
  if (u == t) return uv;
  long long used = 0;
  for (long long i = cur[u]; ~i; i = a[i].next) {
    cur[u] = i;
    long long v = a[i].v;
    long long val = a[i].val;
    if (dep[v] == dep[u] + 1 && val) {
      long long res = dfs(v, min(uv - used, val));
      a[i].val -= res;
      a[i ^ 1].val += res;
      used += res;
      if (used == uv) return used;
    }
  }
  if (used == 0) dep[u] = -1;
  return used;
}
long long Dinic(long long s) {
  long long ans = 0;
  while (bfs(s)) ans += dfs(s, inf);
  return ans;
}
void init() {
  cnt = 0;
  for (long long i = 0; i <= 2 * n; i++) head[i] = -1;
}
bool check(long long x) {
  init();
  t = 2 * n + 1;
  for (long long i = 1; i <= n; i++) {
    add(0, i, 1);
    add(i + n, 2 * n + 1, 1);
  }
  for (long long i = 1; i <= m; i++)
    if (b[i].w <= x) add(b[i].u, b[i].v + n, 1);
  if (Dinic(0) < n)
    return false;
  else
    return true;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  long long Max = 0;
  for (long long i = 1; i <= m; i++) {
    cin >> b[i].u >> b[i].v >> b[i].w;
    Max = max(Max, b[i].w);
  }
  bool flag = 0;
  long long l = 0, r = Max;
  while (l < r) {
    long long mid = (l + r) / 2;
    if (check(mid)) {
      r = mid;
      flag = 1;
    } else
      l = mid + 1;
  }
  if (flag)
    cout << l << endl;
  else
    cout << "-1" << endl;
  return 0;
}
