#include <bits/stdc++.h>
using namespace std;
const long long int inf = 1e12;
const int N = 1e6;
class BipGraph {
  int m, n;
  list<int> *adj;
  int *pairU, *pairV, *dist;

 public:
  BipGraph(int m, int n);
  void addEdge(int u, int v);
  void removeEdge(int u);
  bool bfs();
  bool dfs(int u);
  int hopcroftKarp();
};
int BipGraph::hopcroftKarp() {
  pairU = new int[m + 1];
  pairV = new int[n + 1];
  dist = new int[m + 1];
  for (int u = 0; u <= m; u++) pairU[u] = 0;
  for (int v = 0; v <= n; v++) pairV[v] = 0;
  int result = 0;
  while (bfs()) {
    for (int u = 1; u <= m; u++)
      if (pairU[u] == 0 && dfs(u)) result++;
  }
  return result;
}
bool BipGraph::bfs() {
  queue<int> Q;
  for (int u = 1; u <= m; u++) {
    if (pairU[u] == 0) {
      dist[u] = 0;
      Q.push(u);
    } else
      dist[u] = INT_MAX;
  }
  dist[0] = INT_MAX;
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    if (dist[u] < dist[0]) {
      list<int>::iterator i;
      for (i = adj[u].begin(); i != adj[u].end(); ++i) {
        int v = *i;
        if (dist[pairV[v]] == INT_MAX) {
          dist[pairV[v]] = dist[u] + 1;
          Q.push(pairV[v]);
        }
      }
    }
  }
  return (dist[0] != INT_MAX);
}
bool BipGraph::dfs(int u) {
  if (u != 0) {
    list<int>::iterator i;
    for (i = adj[u].begin(); i != adj[u].end(); ++i) {
      int v = *i;
      if (dist[pairV[v]] == dist[u] + 1) {
        if (dfs(pairV[v]) == true) {
          pairV[v] = u;
          pairU[u] = v;
          return true;
        }
      }
    }
    dist[u] = INT_MAX;
    return false;
  }
  return true;
}
BipGraph::BipGraph(int m, int n) {
  this->m = m;
  this->n = n;
  adj = new list<int>[m + 1];
}
void BipGraph::addEdge(int u, int v) { adj[u].push_back(v); }
void BipGraph::removeEdge(int u) { adj[u].clear(); }
bool check(vector<pair<int, pair<int, int>>> v, long long int n,
           long long int mid) {
  int i, j, val;
  BipGraph g(n, n);
  for (i = 1; i <= n; i++) {
    g.removeEdge(i);
  }
  for (i = 0; i < v.size(); i++) {
    if (v[i].first > mid) {
      break;
    }
    g.addEdge(v[i].second.first, v[i].second.second);
  }
  val = g.hopcroftKarp();
  if (val == n) {
    return true;
  }
  return false;
}
void solve() {
  int i, j;
  int n, m, u, v;
  long long int d;
  cin >> n >> m;
  vector<pair<int, pair<int, int>>> vect(m);
  pair<int, pair<int, int>> p;
  for (i = 0; i < m; i++) {
    cin >> u >> v >> d;
    p.first = d;
    p.second.first = u;
    p.second.second = v;
    vect[i] = p;
  }
  sort(vect.begin(), vect.end());
  long long int left, right, mid, ans = inf;
  left = vect[0].first;
  right = vect[m - 1].first;
  while (left <= right) {
    mid = (left + right) / 2;
    if (check(vect, n, mid)) {
      ans = mid;
      right = mid - 1;
    } else {
      left = mid + 1;
    }
  }
  if (ans == inf) {
    ans = -1;
  }
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
}
