#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
const int N = 2e4 + 5, M = 1000000007, INF = 0x3f3f3f3f;
const ll LNF = 0x3f3f3f3f3f3f3f3f;
void Main();
void debug() { cerr << endl; }
template <typename T, typename... Ts>
void debug(T x, Ts... y) {
  cerr << "\033[31m" << x << "\033[0m";
  if (sizeof...(y) > 0) cerr << ' ';
  debug(y...);
}
template <typename T>
void debug(const T& a, int l, int r, char c) {
  for (int i = l; i <= r; ++i)
    cerr << "\033[31m" << a[i] << "\033[0m" << (i == r ? '\n' : c);
}
template <typename T>
void debug(vector<T> a) {
  for (int i = 0; i < (int)a.size(); ++i)
    cout << "\033[31m" << a[i] << "\033[0m"
         << " \n"[i == (int)a.size() - 1];
}
void print() { cout << '\n'; }
template <typename T, typename... Ts>
void print(T x, Ts... y) {
  cout << x;
  if (sizeof...(y) > 0) cout << ' ';
  print(y...);
}
template <typename T>
void print(const T& a, int l, int r, char c) {
  for (int i = l; i <= r; ++i) cout << a[i] << (i == r ? '\n' : c);
}
template <typename T>
void print(vector<T> a) {
  for (int i = 0; i < (int)a.size(); ++i)
    cout << a[i] << " \n"[i == (int)a.size() - 1];
}
void input() {}
template <typename T, typename... Ts>
void input(T& x, Ts&... y) {
  cin >> x;
  input(y...);
}
template <typename T>
void input(T* a, int l, int r) {
  for (int i = l; i <= r; ++i) cin >> a[i];
}
template <typename T>
void input(vector<T>& a, int l, int r) {
  for (int i = l; i <= r; ++i) cin >> a[i];
}
template <typename T>
void input(vector<T> a) {
  for (auto&& x : a) input(a);
}
template <typename T>
void Max(T& a, T b) {
  if (a < b) a = b;
}
template <typename T>
void Min(T& a, T b) {
  if (a > b) a = b;
}
template <typename T>
void Add(T& a, T b) {
  a += b;
  if (a > M) a -= M;
  if (a < 0) a += M;
}
template <typename T>
void Mul(T& a, T b) {
  a = a * b % M;
}
template <typename T>
T Mod(T a) {
  return (a % M + M) % M;
}
ll qpow(ll a, ll b) {
  ll ret = 1;
  while (b) {
    if (b & 1) ret = ret * a % M;
    a = a * a % M, b >>= 1;
  }
  return ret;
}
ll pinv(ll x, ll mod = M) { return qpow(x, mod - 2); }
int main(int argc, char** argv) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  Main();
}
template <typename T, int N, T oo>
struct Dinic {
  int n, s, t;
  struct edge {
    int v;
    T w;
    int rev;
  };
  vector<edge> G[N];
  void add(int u, int v, T w) {
    G[u].push_back({v, w, (int)G[v].size()});
    G[v].push_back({u, 0, (int)G[u].size() - 1});
  }
  void init(int n_) {
    n = n_;
    for (int i = 0; i <= n; i++) G[i].clear();
  }
  int dis[N], cur[N];
  bool bfs() {
    fill(dis, dis + n + 1, -1);
    fill(cur, cur + n + 1, 0);
    dis[s] = 0;
    queue<int> q;
    q.push(s);
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      for (edge& e : G[u])
        if (e.w && dis[e.v] == -1) {
          dis[e.v] = dis[u] + 1;
          q.push(e.v);
        }
    }
    return dis[t] != -1;
  }
  T dfs(int u, T f) {
    if (u == t) return f;
    T used = 0;
    for (int& i = cur[u]; i < (int)G[u].size() && f > used; i++) {
      int v = G[u][i].v;
      T &w = G[u][i].w, &revw = G[v][G[u][i].rev].w;
      if (dis[v] != dis[u] + 1 || w <= 0) continue;
      T t = dfs(v, min(f - used, w));
      used += t;
      w -= t;
      revw += t;
    }
    return used;
  }
  T maxflow(int s_, int t_) {
    s = s_, t = t_;
    T ans = 0;
    while (bfs()) ans += dfs(s, oo);
    return ans;
  }
};
Dinic<int, N, INF> F;
struct edge {
  int u, v, w;
} a[(int)1e5 + 5];
void Main() {
  int n, m;
  input(n, m);
  for (int i = 1; i <= m; ++i) input(a[i].u, a[i].v, a[i].w);
  int l = 1, r = m, ans = -1;
  const int S = n * 2 + 1, T = S + 1;
  sort(a + 1, a + m + 1,
       [](const edge& x, const edge& y) { return x.w < y.w; });
  while (l <= r) {
    F.init(n * 2 + 2);
    int mid = (l + r) / 2;
    for (int i = 1; i <= mid; ++i) F.add(a[i].u, a[i].v + n, 1);
    for (int i = 1; i <= n; ++i) F.add(S, i, 1);
    for (int i = 1; i <= n; ++i) F.add(i + n, T, 1);
    if (F.maxflow(S, T) != n) {
      l = mid + 1;
    } else {
      r = mid - 1;
      ans = mid;
    }
  }
  print(ans == -1 ? -1 : a[ans].w);
}
