#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
long long fact[200000];
long long dmod(long long x) { return ((x + mod) % mod); }
long long power(long long x, long long y) {
  long long res = 1;
  while (y) {
    if (y & 1) res = (res * x) % mod;
    x = (x * x) % mod;
    y >>= 1;
  }
  return res;
}
long long nCr(long long n, long long r) {
  if (r > n)
    return 0;
  else if (r < 0)
    return 0;
  else if (r == 0 || r == n)
    return 1;
  long long ans = fact[n];
  ans = (ans * power(fact[n - r], mod - 2)) % mod;
  ans = (ans * power(fact[r], mod - 2)) % mod;
  return ans;
}
struct FlowEdge {
  int v, u;
  long long cap, flow = 0;
  FlowEdge(int v, int u, long long cap) : v(v), u(u), cap(cap) {}
};
struct Dinic {
  const long long flow_inf = 1e18;
  vector<FlowEdge> edges;
  vector<vector<int>> adj;
  int n, m = 0;
  int s, t;
  vector<int> level, ptr;
  queue<int> q;
  Dinic(int n, int s, int t) : n(n), s(s), t(t) {
    adj.resize(n + 1);
    level.resize(n + 1);
    ptr.resize(n + 1);
  }
  void add_edge(int v, int u, long long cap) {
    edges.emplace_back(v, u, cap);
    edges.emplace_back(u, v, 0);
    adj[v].push_back(m);
    adj[u].push_back(m + 1);
    m += 2;
  }
  bool bfs() {
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      for (int id : adj[v]) {
        if (edges[id].cap - edges[id].flow < 1) continue;
        if (level[edges[id].u] != -1) continue;
        level[edges[id].u] = level[v] + 1;
        q.push(edges[id].u);
      }
    }
    return level[t] != -1;
  }
  long long dfs(int v, long long pushed) {
    if (pushed == 0) return 0;
    if (v == t) return pushed;
    for (int& cid = ptr[v]; cid < (int)adj[v].size(); cid++) {
      int id = adj[v][cid];
      int u = edges[id].u;
      if (level[v] + 1 != level[u] || edges[id].cap - edges[id].flow < 1)
        continue;
      long long tr = dfs(u, min(pushed, edges[id].cap - edges[id].flow));
      if (tr == 0) continue;
      edges[id].flow += tr;
      edges[id ^ 1].flow -= tr;
      return tr;
    }
    return 0;
  }
  long long flow() {
    long long f = 0;
    while (true) {
      fill(level.begin(), level.end(), -1);
      level[s] = 0;
      q.push(s);
      if (!bfs()) break;
      fill(ptr.begin(), ptr.end(), 0);
      while (long long pushed = dfs(s, flow_inf)) {
        f += pushed;
      }
    }
    return f;
  }
};
vector<long long> BIT;
long long query(long long i) {
  long long res = 0;
  while (i >= 0) {
    res += BIT[i];
    i = (i & (i + 1)) - 1;
  }
  return res;
}
void update(long long i, long long val) {
  while (i < BIT.size()) {
    BIT[i] += val;
    i = (i | (i + 1));
  }
}
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);
  }
};
template <class T, class H>
using umap = unordered_map<T, H, custom_hash>;
template <class T>
using uset = unordered_set<T, custom_hash>;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n, m;
  cin >> n >> m;
  vector<pair<pair<long long, long long>, long long>> vec;
  for (long long i = 0; i < m; i++) {
    long long a, b, c;
    cin >> a >> b >> c;
    vec.push_back({{a, b}, c});
  }
  long long low = 0, high = 1e9, ans = 1e9;
  while (low <= high) {
    long long mid = (high + low) / 2;
    Dinic d(2 * n + 2, 0, 2 * n + 1);
    for (long long i = 1; i <= n; i++) {
      d.add_edge(0, i, 1);
      d.add_edge(n + i, 2 * n + 1, 1);
    }
    for (int i = 0; i < vec.size(); i++) {
      if (vec[i].second > mid) continue;
      d.add_edge(vec[i].first.first, vec[i].first.second + n, 1);
    }
    long long res = d.flow();
    if (res == n) {
      ans = mid;
      high = mid - 1;
    } else
      low = mid + 1;
  }
  if (ans == 1e9)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}
