#include <bits/stdc++.h>
using namespace std;
inline int lowbit(int x) { return x & (-x); }
struct Dinic {
  int vis[20100], dis[20100], cur[20100];
  struct Edge {
    int from, to, cap, flow, next, cost;
  } edges[300100 * 2];
  int head[20100];
  int n, m, s, t, b_cnt;
  int LiM;
  inline Dinic() {
    b_cnt = -1;
    memset(head, -1, sizeof(head));
    memset(edges, 0, sizeof(edges));
  }
  inline void init(int n, int m, int s, int t, int mid) {
    this->n = n;
    this->m = m;
    this->s = s;
    this->t = t;
    this->LiM = mid;
    b_cnt = -1;
    memset(head, -1, sizeof(head));
    memset(edges, 0, sizeof(edges));
  }
  inline void addedge(int u, int v, int cap, int cost) {
    edges[++b_cnt].next = head[u];
    edges[b_cnt].to = v;
    edges[b_cnt].from = u;
    edges[b_cnt].cap = cap;
    edges[b_cnt].cost = cost;
    edges[b_cnt].flow = 0;
    head[u] = b_cnt;
    edges[++b_cnt].next = head[v];
    edges[b_cnt].to = u;
    edges[b_cnt].from = v;
    edges[b_cnt].cost = 0;
    edges[b_cnt].cap = 0;
    edges[b_cnt].flow = 0;
    head[v] = b_cnt;
  }
  inline bool BFS() {
    queue<int> Q;
    memset(vis, 0, sizeof(vis));
    memset(dis, 0x3f3f3f3f, sizeof(dis));
    Q.push(s);
    vis[s] = 1;
    dis[s] = 0;
    while (!Q.empty()) {
      int u = Q.front();
      Q.pop();
      for (int i = head[u]; i != -1; i = edges[i].next) {
        int v = edges[i].to;
        Edge &e = edges[i];
        if (e.cap > e.flow && !vis[v]) {
          Q.push(v);
          vis[v] = 1;
          dis[v] = dis[u] + 1;
        }
      }
    }
    return vis[t];
  }
  inline int dfs(int u, int a) {
    if (u == t || a == 0) return a;
    int flow = 0, f;
    for (int &i = cur[u]; i != -1; i = edges[i].next) {
      Edge &e = edges[i];
      int v = e.to;
      if (dis[v] == dis[u] + 1 && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0) {
        e.flow += f;
        edges[i ^ 1].flow -= f;
        flow += f;
        a -= f;
      }
      if (a == 0) break;
    }
    return flow;
  }
  inline int Maxflow() {
    int flow = 0;
    while (BFS()) {
      for (int i = 0; i <= n + 1; i++) cur[i] = head[i];
      flow += dfs(s, 0x3f3f3f3f);
    }
    return flow;
  }
};
Dinic A;
int n, m, s, t;
int U[300100], V[300100], Cost[300100];
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) cin >> U[i] >> V[i] >> Cost[i];
  int L = 0, R = 0;
  for (int i = 1; i <= m; i++) R = max(R, Cost[i]);
  while (L < R) {
    int mid = (L + R) >> 1;
    int s = n * 2 + 1, t = n * 2 + 2;
    A.init(n * 2 + 2, m, s, t, mid);
    for (int i = 1; i <= m; i++) {
      int u, v, cap, cost;
      u = U[i], v = V[i], cost = Cost[i];
      v = v + n;
      cap = 1;
      if (cost <= mid) A.addedge(u, v, cap, cost);
    }
    for (int i = 1; i <= n; i++) {
      A.addedge(s, i, 1, 0);
      A.addedge(n + i, t, 1, 0);
    }
    int ans = A.Maxflow();
    if (ans == n)
      R = mid;
    else
      L = mid + 1;
  }
  int mid = R;
  int s = n * 2 + 1, t = n * 2 + 2;
  A.init(n * 2 + 2, m, s, t, mid);
  for (int i = 1; i <= m; i++) {
    int u, v, cap, cost;
    u = U[i], v = V[i], cost = Cost[i];
    v = v + n;
    cap = 1;
    A.addedge(u, v, cap, cost);
  }
  for (int i = 1; i <= n; i++) {
    A.addedge(s, i, 1, 0);
    A.addedge(n + i, t, 1, 0);
  }
  int ans = A.Maxflow();
  if (ans != n)
    puts("-1");
  else
    cout << R << endl;
  return 0;
}
