#include <bits/stdc++.h>
const double eps = 1e-7, PI = 3.1415926;
const int N = 1e6 + 10;
using namespace std;
int n, q, m, k, x, y, a[N], mx = -1, mn = 1e9, sum, p[N], sz[N], vis[N];
char c[N];
string s, s1, s2;
map<int, int> mp;
vector<pair<int, pair<int, int> > > vec;
int find(int x) {
  if (p[x] == x)
    return x;
  else
    return p[x] = find(p[x]);
}
void join(int u, int v) {
  u = find(u);
  v = find(v);
  if (u == v) return;
  if (sz[v] > sz[u]) swap(u, v);
  p[v] = u;
  sz[u] += sz[v];
  return;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= 2 * n; i++) {
    p[i] = i;
    sz[i] = 1;
  }
  while (m--) {
    cin >> x >> y >> k;
    y += n;
    vec.push_back({k, {x, y}});
  }
  sort(vec.begin(), vec.end());
  for (auto x : vec) {
    if (sum >= 2 * n) break;
    if (find(x.second.second) != find(x.second.first)) {
      if (!vis[x.second.second]) sum++;
      if (!vis[x.second.first]) sum++;
      vis[x.second.second] = 1;
      vis[x.second.first] = 1;
      join(x.second.first, x.second.second);
      mx = max(mx, x.first);
    }
  }
  if (sum >= 2 * n)
    cout << mx;
  else
    cout << -1;
  return 0;
}
