#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
int T, n, m, tot, totl, ans, dis;
struct node {
  int per, to, w, tag;
} e[20010 * 10];
int head[20010 * 10], vis[20010];
int dx[20010], dy[20010], xlink[20010], ylink[20010];
void addedge(int from, int to, int w) {
  e[++tot].to = to;
  e[tot].per = head[from];
  e[tot].w = w;
  head[from] = tot;
}
int bfs() {
  queue<int> q;
  dis = 0x3f3f3f3f;
  memset(dx, -1, sizeof(dx));
  memset(dy, -1, sizeof(dy));
  for (int i = 1; i <= totl; i++) {
    if (xlink[i] == -1) {
      q.push(i);
      dx[i] = 0;
    }
  }
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    if (dx[u] > dis) break;
    for (int i = head[u]; i > 0; i = e[i].per) {
      int v = e[i].to;
      if (e[i].tag) continue;
      if (dy[v] == -1) {
        dy[v] = dx[u] + 1;
        if (ylink[v] == -1)
          dis = dy[v];
        else {
          dx[ylink[v]] = dy[v] + 1;
          q.push(ylink[v]);
        }
      }
    }
  }
  return dis != 0x3f3f3f3f;
}
int find(int u) {
  for (int i = head[u]; i > 0; i = e[i].per) {
    int v = e[i].to;
    if (e[i].tag) continue;
    if (!vis[v] && dy[v] == dx[u] + 1) {
      vis[v] = 1;
      if (ylink[v] != -1 && dy[v] == dis) continue;
      if (ylink[v] == -1 || find(ylink[v])) {
        xlink[u] = v, ylink[v] = u;
        return 1;
      }
    }
  }
  return 0;
}
void MaxMatch() {
  while (bfs()) {
    memset(vis, 0, sizeof(vis));
    for (int i = 1; i <= totl; i++)
      if (xlink[i] == -1) {
        ans += find(i);
      }
  }
}
bool check(int num) {
  for (int i = 1; i <= n; ++i) {
    xlink[i] = ylink[i] = -1;
    vis[i] = 0;
  }
  for (int i = 1; i <= tot; ++i) {
    e[i].tag = (e[i].w > num);
  }
  ans = 0;
  MaxMatch();
  return ans == n;
}
int main() {
  T = 1;
  while (T--) {
    scanf("%d%d", &n, &m);
    totl = n;
    for (int i = 1; i <= m; ++i) {
      int u, v, w;
      scanf("%d%d%d", &u, &v, &w);
      addedge(u, v, w);
    }
    int l = 1, r = 1e9, d = -1;
    while (l <= r) {
      int mid = (l + r) >> 1;
      if (check(mid)) {
        d = mid;
        r = mid - 1;
      } else {
        l = mid + 1;
      }
    }
    printf("%d\n", d);
  }
  return 0;
}
