#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
mt19937_64 rnd64(
    chrono::high_resolution_clock::now().time_since_epoch().count());
long long power(long long a, long long m, long long mod) {
  long long ans = 1;
  while (m) {
    if (m % 2) ans *= a, ans %= (mod);
    a = (a * a) % mod;
    m >>= 1;
  }
  return ans;
}
struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
  size_t operator()(pair<int64_t, int64_t> Y) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(Y.first * 31 + Y.second + FIXED_RANDOM);
  }
};
const long long inf = 1e18;
const long long M = 1000000007;
struct hopcroft_karp {
  const long long now_inf = 1e9 + 7;
  long long n, m;
  vector<long long> ml, mr, dist;
  vector<vector<long long> > adj;
  list<long long> queue;
  hopcroft_karp(long long n, long long m) {
    this->m = m;
    this->n = n;
    dist.resize(n + 1);
    ml.resize(n + 1, 0);
    mr.resize(m + 1, 0);
    adj.resize(n + 1);
  }
  void addEdge(long long u, long long v) { adj[u].push_back(v); }
  bool bfs() {
    long long i;
    queue.clear();
    dist[0] = now_inf;
    for (i = 1; i < n + 1; i++) {
      if (!ml[i])
        queue.push_back(i), dist[i] = 0;
      else
        dist[i] = now_inf;
    }
    while (!queue.empty()) {
      long long z = queue.front();
      queue.pop_front();
      for (auto &i : adj[z])
        if (dist[mr[i]] == now_inf)
          queue.push_back(mr[i]), dist[mr[i]] = dist[z] + 1;
    }
    return dist[0] != now_inf;
  }
  bool dfs(long long u) {
    if (!u) return 1;
    for (auto &i : adj[u]) {
      if (dist[mr[i]] == dist[u] + 1 && dfs(mr[i])) {
        ml[u] = i, mr[i] = u;
        return 1;
      }
    }
    dist[u] = now_inf;
    return 0;
  }
  long long match() {
    long long ans = 0, i;
    while (bfs())
      for (i = 1; i < n + 1; i++)
        if (!ml[i] && dfs(i)) ++ans;
    return ans;
  }
};
void solve() {
  long long i, j, _;
  long long n, m;
  cin >> n >> m;
  vector<pair<long long, pair<long long, long long> > > v(m);
  for (i = 0; i < m; i++)
    cin >> v[i].second.first >> v[i].second.second >> v[i].first;
  long long l = 1, r = 1e9 + 1;
  long long ans = r;
  while (l <= r) {
    long long mid = (l + r) / 2;
    hopcroft_karp flow(n, n);
    for (i = 0; i < m; i++) {
      if (v[i].first <= mid) {
        flow.addEdge(v[i].second.first, v[i].second.second);
      }
    }
    long long now = flow.match();
    if (now < n) {
      l = mid + 1;
    } else {
      ans = min(ans, mid);
      r = mid - 1;
    }
  }
  if (ans == 1e9 + 1) ans = -1;
  cout << ans << '\n';
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long T = 1;
  while (T--) {
    solve();
  }
  return 0;
}
