#include <bits/stdc++.h>
const int maxn = 100005;
const int inf = 1e9 + 7;
using namespace std;
int n, m;
vector<int> g[maxn];
int Fx[maxn], Fy[maxn], d[maxn], q[maxn];
pair<int, pair<int, int> > e[maxn];
bool FindPath() {
  int L = 1, R = 0;
  for (int u = 1; u <= n; ++u) {
    if (Fx[u] == 0) {
      d[u] = 0;
      q[++R] = u;
    } else
      d[u] = inf;
  }
  d[0] = inf;
  while (L <= R) {
    int u = q[L++];
    for (int v : g[u]) {
      if (d[Fy[v]] == inf) {
        d[Fy[v]] = d[u] + 1;
        if (Fy[v]) q[++R] = Fy[v];
      }
    }
  }
  return (d[0] != inf);
}
bool Match(int u) {
  if (u == 0) return true;
  for (int v : g[u]) {
    if (d[Fy[v]] == d[u] + 1)
      if (Match(Fy[v])) {
        Fx[u] = v;
        Fy[v] = u;
        return true;
      }
  }
  d[u] = inf;
  return false;
}
bool Solve(int first) {
  for (int i = 1; i <= n; ++i) g[i].clear();
  for (int i = 1; i <= m; ++i) {
    if (e[i].first <= first) {
      g[e[i].second.first].push_back(e[i].second.second);
    }
  }
  int mf = 0;
  memset(Fx, 0, sizeof(Fx));
  memset(Fy, 0, sizeof(Fy));
  while (FindPath()) {
    for (int u = 1; u <= n; ++u)
      if (Fx[u] == 0)
        if (Match(u)) ++mf;
  }
  return mf == n;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    int u, v, d;
    cin >> u >> v >> d;
    e[i] = {d, {u, v}};
  }
  int lo = 0, hi = inf;
  while (hi - lo > 1) {
    int mid = (hi + lo) / 2;
    if (Solve(mid))
      hi = mid;
    else
      lo = mid;
  }
  cout << ((Solve(hi)) ? hi : -1);
}
