#include <bits/stdc++.h>
using namespace std;
const long long INF = 9223372036854775807;
const long long MOD = 1000000007;
const long double PI = acos(-1);
inline long long power(long long a, long long b, long long z = MOD) {
  long long res = 1;
  a = (a % z);
  while (b) {
    if (b & 1) res = (res * a) % z;
    a = (a * a) % z;
    b = b >> 1;
  }
  return res;
}
inline long long inverse(long long a, long long z = MOD) {
  return power(a, z - 2, z);
}
long long extend(long long a, long long b, long long &x, long long &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  long long x1, y1;
  long long g = extend(b, a % b, x1, y1);
  x = y1;
  y = x1 - (a / b) * y1;
  return g;
}
class matrix {
 public:
  long long n, aray[5][5], z = MOD;
  matrix(long long n) {
    this->n = n;
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < n; j++) aray[i][j] = (i == j);
    }
  }
  matrix operator+(const matrix &a) {
    matrix ans = matrix(n);
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < n; j++)
        ans.aray[i][j] = (a.aray[i][j] + aray[i][j]) % z;
    }
    return ans;
  }
  matrix operator*(const matrix &a) {
    matrix ans = matrix(n);
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < n; j++) {
        ans.aray[i][j] = 0;
        for (long long k = 0; k < n; k++)
          ans.aray[i][j] = (ans.aray[i][j] + aray[i][k] * a.aray[k][j]) % z;
      }
    }
    return ans;
  }
};
inline matrix mat_power(matrix x, long long b, long long z = MOD) {
  matrix ans = matrix(x.n);
  while (b) {
    if (b & 1) ans = ans * x;
    x = x * x;
    b = b >> 1;
  }
  return ans;
}
vector<vector<int> > edges;
const long long MAX = 2e5 + 1;
vector<int> arr[MAX], visited(MAX, 0), visit(MAX, 1e7);
vector<int> path;
bool mila;
void dfs(int root) {
  visited[root] = 1;
  path.push_back(root);
  if (visit[root] == 0) {
    mila = true;
    return;
  }
  for (auto x : arr[root]) {
    if (mila) return;
    if (!visited[x] && visit[x] == visit[root] - 1) dfs(x);
  }
  if (!mila) return;
  path.pop_back();
  return;
}
bool hopcraft(int m, int n) {
  m++;
  vector<int> aray[2 * n + 1], sat(2 * n + 1, 0);
  for (int i = 0; i < m; i++) {
    int x = edges[i][1], y = edges[i][2];
    y += n;
    aray[x].push_back(y);
    aray[y].push_back(x);
  }
  while (1) {
    for (auto &x : visited) x = 0;
    for (auto &x : arr) x.clear();
    for (auto &x : visit) x = 1e7;
    queue<int> q;
    for (int i = 1; i <= n; i++) {
      if (!sat[i]) {
        visit[i] = 0;
        q.push(i);
      }
    }
    vector<int> f;
    while (q.size()) {
      int vert = q.front();
      q.pop();
      if (vert <= n) {
        for (auto x : aray[vert]) {
          if (visit[x] < visit[vert] + 1) continue;
          arr[x].push_back(vert);
          arr[vert].push_back(x);
          if (visit[x] > visit[vert] + 1) q.push(x);
          visit[x] = visit[vert] + 1;
        }
      } else {
        if (sat[vert]) {
          int x = sat[vert];
          if (visit[x] < visit[vert] + 1) continue;
          arr[x].push_back(vert);
          arr[vert].push_back(x);
          if (visit[x] > visit[vert] + 1) q.push(x);
          visit[x] = visit[vert] + 1;
        } else
          f.push_back(vert);
      }
    }
    if (f.size() == 0) break;
    for (auto x : f) {
      mila = false;
      path.clear();
      dfs(x);
      if (!mila) continue;
      for (int i = 0; i < path.size(); i += 2) {
        sat[path[i]] = path[i + 1];
        sat[path[i + 1]] = path[i];
      }
    }
  }
  int sum = 0;
  for (int i = 1; i <= n; i++) sum += (sat[i] > 0);
  return (sum == n);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  int t = m;
  while (m--) {
    int x, y, z;
    cin >> x >> y >> z;
    edges.push_back({z, x, y});
  }
  sort(edges.begin(), edges.end());
  int left = 0, right = t - 1, mid;
  while (left < right) {
    mid = (left + right) >> 1;
    if (hopcraft(mid, n))
      right = mid;
    else
      left = mid + 1;
  }
  if (hopcraft(left, n))
    cout << edges[left][0] << "\n";
  else
    cout << -1 << "\n";
  return 0;
}
