#include <bits/stdc++.h>
using namespace std;
template <typename U, typename V>
void Min(U &a, const V &b) {
  if (a > b) a = b;
}
template <typename U, typename V>
void Max(U &a, const V &b) {
  if (a < b) a = b;
}
template <typename U, typename V>
void add(U &a, const V &b) {
  a = (a + b) % 1000000007;
}
template <typename U>
U gcd(U a, U b) {
  if (a == 0) return b;
  if (b == 0) return a;
  if (a >= b)
    return gcd(a % b, b);
  else
    return gcd(a, b % a);
}
int pow(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1) ans = 1LL * ans * a % 1000000007;
    a = 1LL * a * a % 1000000007;
    b >>= 1;
  }
  return ans;
}
int pow(int a, long long b, int c) {
  int ans = 1;
  while (b) {
    if (b & 1) ans = 1LL * ans * a % c;
    a = 1LL * a * a % c;
    b >>= 1;
  }
  return ans;
}
vector<int> g[10005];
int now, vis[10005], is[10005];
int find(int u) {
  for (auto &v : g[u]) {
    if (is[v] == now) continue;
    is[v] = now;
    if (vis[v] == -1 || find(vis[v])) {
      vis[v] = u;
      return 1;
    }
  }
  return 0;
}
struct node {
  int u, v, w;
  bool operator<(const node &a) const { return w < a.w; }
} e[10005 * 10];
int main() {
  int ca = 0, T, i, j, k, n, K, m;
  scanf("%d%d", &n, &m);
  vector<int> q;
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &j, &k, &K);
    j--, k--;
    e[i] = {j, k, K};
  }
  sort(e, e + m);
  for (int i = 0; i < m; i++) {
    g[e[i].u].push_back(e[i].v);
  }
  for (int j = 0; j < n; j++) vis[j] = -1;
  for (int i = 0; i < n; i++) {
    now++;
    if (!find(i)) {
      puts("-1");
      return 0;
    }
  }
  int ans = e[m - 1].w;
  for (int i = m - 1; i >= 0; i--) {
    int u = e[i].u, v = e[i].v;
    g[u].pop_back();
    if (vis[v] == u) {
      vis[v] = -1;
      now++;
      if (!find(u)) break;
    }
    if (i) ans = e[i - 1].w;
  }
  printf("%d\n", ans);
}
