#include <bits/stdc++.h>
using namespace std;
vector<tuple<long long int, long long int, long long int>> edges;
long long int n, m;
long long int f(long long int x) {
  long long int arr1[n];
  long long int arr2[n];
  memset(arr1, 0, sizeof(arr1));
  memset(arr2, 0, sizeof(arr2));
  for (auto j : edges) {
    if ((get<2>(j)) <= x) {
      long long int u = get<0>(j);
      long long int v = get<1>(j);
      u--;
      v--;
      arr1[u] = 1;
      arr2[v] = 1;
    }
  }
  long long int poss = 1;
  for (long long int i = 0; i < n; i++) {
    if (arr1[i] == 0 || arr2[i] == 0) {
      poss = 0;
      break;
    }
  }
  return poss;
}
long long int bsearch(long long int l, long long int r) {
  long long int m = (l + r) / 2;
  if (l > r) {
    return -1;
  }
  if (f(m)) {
    if (f(m - 1) == 0) {
      return m;
    }
    return bsearch(l, m - 1);
  }
  return bsearch(m + 1, r);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  long long int num1, num2, num3;
  for (long long int i = 0; i < m; i++) {
    cin >> num1 >> num2 >> num3;
    edges.push_back(make_tuple(num1, num2, num3));
  }
  cout << bsearch(0, 1e15) << "\n";
}
