#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<pair<int, pair<int, int> > > v;
  set<int> s1, s2;
  for (int i = 0; i < n; ++i) {
    s1.insert(i + 1);
    s2.insert(i + 1);
  }
  for (int i = 0; i < m; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    v.push_back(make_pair(c, make_pair(a, b)));
  }
  sort(v.begin(), v.end());
  int z = -1;
  for (int i = 0; i < m; ++i) {
    s1.erase(v[i].second.first);
    s2.erase(v[i].second.second);
    if (s1.empty() and s2.empty()) {
      z = v[i].first;
      break;
    }
  }
  cout << z;
}
