#include <bits/stdc++.h>
using namespace std;
class Graph {
 private:
  int m, n;
  vector<vector<pair<int, int>>> ad;
  vector<int> matchR, matchL;
  bool match(int node, vector<bool>& vis, int mx) {
    if (vis[node]) return false;
    vis[node] = true;
    for (auto nghb : ad[node]) {
      if (nghb.first > mx) break;
      if (!matchL[nghb.second]) {
        matchR[node] = nghb.second;
        matchL[nghb.second] = node;
        return true;
      }
    }
    for (auto nghb : ad[node]) {
      if (nghb.first > mx) break;
      if (match(matchL[nghb.second], vis, mx)) {
        matchR[node] = nghb.second;
        matchL[nghb.second] = node;
        return true;
      }
    }
    return false;
  }

 public:
  Graph(int m, int n) : m(m), n(n), ad(m + 1) {}
  void addEdge(int x, int y, int z) { ad[x].emplace_back(z, y); }
  void sortAd() {
    for (int i = 1; i <= m; i++) sort(ad[i].begin(), ad[i].end());
  }
  bool matching(int mx) {
    matchR.assign(m + 1, 0);
    matchL.assign(n + 1, 0);
    bool toDo;
    do {
      toDo = false;
      vector<bool> vis(m + 1);
      for (int i = 1; i <= m; i++)
        if (!matchR[i]) toDo |= match(i, vis, mx);
    } while (toDo);
    int matched = 0;
    for (int i = 1; i <= m; i++) matched += (bool)matchR[i];
    return matched == m;
  }
  int getMin() {
    int mn = 0;
    for (int i = 1; i <= m; i++)
      if (ad[i].size() == 1) mn = max(mn, ad[i][0].first);
    return mn;
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  Graph graph(n, n);
  vector<int> val;
  for (int i = 0; i < m; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    graph.addEdge(x, y, z);
    val.push_back(z);
  }
  graph.sortAd();
  sort(val.begin(), val.end());
  int lo =
          lower_bound(val.begin(), val.end(), graph.getMin()) - val.begin() - 1,
      hi = val.size();
  int ans = -1;
  while (hi - lo > 1) {
    int md = (lo + hi) / 2;
    if (graph.matching(val[md]))
      ans = val[hi = md];
    else
      lo = md;
  }
  cout << ans << '\n';
  return 0;
}
