#include <bits/stdc++.h>
using namespace std;
int U[400005], V[400005], d[400005], sort_ptr[400005], flow[400005],
    cap[400005], level[400005], des[400005], now[400005], cure, n, m;
vector<int> flownet[400005];
void add_edge(int u, int v, int c) {
  des[cure] = v, cap[cure] = c, flow[cure] = 0;
  des[cure ^ 1] = u, cap[cure ^ 1] = 0, flow[cure ^ 1] = 0;
  flownet[u].emplace_back(cure);
  flownet[v].emplace_back(cure ^ 1);
  cure += 2;
}
bool bfs() {
  for (int i = 1; i <= 2 * n + 2; i++) level[i] = INT_MAX;
  level[1] = 0;
  queue<int> q;
  q.push(1);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    if (u == 2 * n + 2) return 1;
    for (auto e_ptr : flownet[u]) {
      int v = des[e_ptr];
      if (flow[e_ptr] < cap[e_ptr] and level[v] > level[u] + 1) {
        level[v] = level[u] + 1;
        q.push(v);
      }
    }
  }
  return 0;
}
int dfs(int u, int curf) {
  if (u == 2 * n + 2) return curf;
  for (; now[u] < flownet[u].size(); now[u]++) {
    int e_ptr = flownet[u][now[u]];
    int v = des[e_ptr];
    if (flow[e_ptr] < cap[e_ptr] and level[v] == level[u] + 1) {
      int temp = dfs(des[e_ptr], min(curf, cap[e_ptr] - flow[e_ptr]));
      if (temp) {
        flow[e_ptr] += temp;
        flow[e_ptr ^ 1] -= temp;
        return temp;
      }
    }
  }
  return 0;
}
int max_flow() {
  int re = 0;
  while (bfs()) {
    for (int i = 1; i <= 2 * n + 2; i++) now[i] = 0;
    while (int temp = dfs(1, INT_MAX)) re += temp;
  }
  return re;
}
bool check(int x) {
  cure = 0;
  for (int i = 1; i <= 2 * n + 2; i++) flownet[i].clear();
  for (int i = 1; i <= n; i++) {
    add_edge(1, i + 1, 1);
    add_edge(i + n + 1, 2 * n + 2, 1);
  }
  for (int i = 1; i <= x; i++)
    add_edge(U[sort_ptr[i]] + 1, V[sort_ptr[i]] + n + 1, 1);
  if (max_flow() >= n) return 1;
  return 0;
}
bool cmp(int u, int v) { return d[u] < d[v]; }
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> U[i] >> V[i] >> d[i];
    sort_ptr[i] = i;
  }
  sort(sort_ptr + 1, sort_ptr + m + 1, cmp);
  int ll = n, rr = m;
  while (ll < rr) {
    int mid = ll + rr >> 1;
    if (check(mid))
      rr = mid;
    else
      ll = mid + 1;
  }
  cout << (check(ll) ? d[sort_ptr[ll]] : -1);
}
