#include <bits/stdc++.h>
using namespace std;
void __print(int x) { cout << x; }
void __print(long x) { cout << x; }
void __print(long long x) { cout << x; }
void __print(unsigned x) { cout << x; }
void __print(unsigned long x) { cout << x; }
void __print(unsigned long long x) { cout << x; }
void __print(float x) { cout << x; }
void __print(double x) { cout << x; }
void __print(long double x) { cout << x; }
void __print(char x) { cout << '\'' << x << '\''; }
void __print(const char *x) { cout << '\"' << x << '\"'; }
void __print(const string &x) { cout << '\"' << x << '\"'; }
void __print(bool x) { cout << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cout << '{';
  __print(x.first);
  cout << ',';
  __print(x.second);
  cout << '}';
}
template <typename T>
void __print(const T &x) {
  int f = 0;
  cout << '{';
  for (auto &i : x) cout << (f++ ? "," : ""), __print(i);
  cout << "}";
}
void _print() { cout << "\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cout << ", ";
  _print(v...);
}
const int mod = 1e9 + 7, mxn = 2e5 + 1;
int n, m;
vector<int> v[mxn], match(mxn), dist(mxn);
bool bfs() {
  int i, u, x, len;
  queue<int> Q;
  for (i = 1; i <= n; i++) {
    if (match[i] == 0) {
      dist[i] = 0;
      Q.push(i);
    } else
      dist[i] = mxn;
  }
  dist[0] = mxn;
  while (!Q.empty()) {
    u = Q.front();
    Q.pop();
    if (u != 0) {
      len = v[u].size();
      for (i = 0; i < len; i++) {
        x = v[u][i];
        if (dist[match[x]] == mxn) {
          dist[match[x]] = dist[u] + 1;
          Q.push(match[x]);
        }
      }
    }
  }
  return (dist[0] != mxn);
}
bool dfs(int u) {
  int i, x, len;
  if (u != 0) {
    len = v[u].size();
    for (i = 0; i < len; i++) {
      x = v[u][i];
      if (dist[match[x]] == dist[u] + 1) {
        if (dfs(match[x])) {
          match[x] = u;
          match[u] = x;
          return true;
        }
      }
    }
    dist[u] = mxn;
    return false;
  }
  return true;
}
int hopcroft_karp() {
  int matching = 0, i;
  while (bfs())
    for (i = 1; i <= n; i++)
      if (match[i] == 0 and dfs(i)) matching++;
  return matching;
}
int solve() {
  cin >> n >> m;
  vector<pair<int, pair<int, int>>> vp(m);
  for (long long i = (0); i < (m); ++i)
    cin >> vp[i].first >> vp[i].second.first >> vp[i].second.second;
  int l = 0, r = mod;
  while (l < r) {
    int mid = (l + r) / 2;
    for (long long i = (0); i < (mxn); ++i)
      match[i] = 0, dist[i] = 0, v[i].clear();
    for (long long i = (0); i < (m); ++i) {
      if (vp[i].second.second <= mid) {
        v[vp[i].first].push_back(n + vp[i].second.first);
        v[n + vp[i].second.first].push_back(vp[i].first);
      }
    }
    int matching = hopcroft_karp();
    if (matching == n)
      r = mid;
    else
      l = mid + 1;
  }
  if (l == mod) l = -1;
  cout << l << "\n";
  return 0;
}
int main() {
  ios_base::sync_with_stdio(NULL);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
