#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1e9;
const long long mod = (long long)1e9 + 7;
const double pi = acos(-1.0);
const double eps = (double)1e-9;
const int dx[] = {0, 0, 1, 0, -1};
const int dy[] = {0, 1, 0, -1, 0};
const int N = 100500;
int n, m;
pair<int, pair<int, int> > p[N];
struct edge {
  int from, to, cap, flow;
  edge(){};
  edge(int a, int b, int c, int d) {
    from = a;
    to = b;
    cap = c;
    flow = d;
  }
};
vector<edge> e;
vector<int> g[N];
int d[N], ptr[N];
inline void add_edge(int x, int y, int cap) {
  g[x].push_back((int)e.size());
  e.push_back(edge(x, y, cap, 0));
  g[y].push_back((int)e.size());
  e.push_back(edge(y, x, 0, 0));
}
inline bool bfs(int x, int y) {
  for (int i = 0; i <= n + n + 1; i++) d[i] = inf;
  d[x] = 0;
  queue<int> q;
  q.push(x);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (auto i : g[v]) {
      if (d[e[i].from] + 1 < d[e[i].to] && e[i].flow < e[i].cap) {
        d[e[i].to] = d[e[i].from] + 1;
        q.push(e[i].to);
      }
    }
  }
  return (d[y] != inf);
}
int dfs(int v, int fin, int flow = inf) {
  if (v == fin || !flow) return flow;
  while (ptr[v] < (int)g[v].size()) {
    int i = g[v][ptr[v]];
    if (d[e[i].from] + 1 == d[e[i].to] && e[i].flow < e[i].cap) {
      int cur = dfs(e[i].to, fin, min(flow, e[i].cap - e[i].flow));
      if (cur) {
        e[i].flow += cur;
        e[i ^ 1].flow -= cur;
        return cur;
      }
    }
    ptr[v]++;
  }
  return 0;
}
inline int max_flow(int x, int y) {
  int res = 0;
  while (bfs(x, y)) {
    memset(ptr, 0, sizeof(ptr));
    while (1) {
      int add = dfs(x, y);
      if (!add) break;
      res += add;
    }
  }
  return res;
}
inline bool check(int lim) {
  for (int i = 0; i <= n + n + 1; i++) g[i].clear();
  e.clear();
  for (int i = 1; i <= n; i++) {
    add_edge(0, i, 1);
    add_edge(n + i, n + n + 1, 1);
  }
  for (int i = 1; i <= lim; i++) {
    int x = p[i].second.first;
    int y = p[i].second.second;
    add_edge(x, n + y, 1);
  }
  int res = max_flow(0, n + n + 1);
  return res == n;
}
int main() {
  cin.tie(NULL);
  cout.tie(NULL);
  ios_base::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    p[i] = {z, {x, y}};
  }
  sort(p + 1, p + 1 + m);
  int l = 1, r = m, res = -1;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid)) {
      res = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  if (res == -1)
    cout << -1;
  else
    cout << p[res].first;
  return 0;
}
