#include <bits/stdc++.h>
typedef struct {
  int a, b, c;
} mch;
int ult[2 * 10000], next[2 * 100000], nod[2 * 100000];
mch mc[100000];
bool cmp(mch a, mch b) {
  if (a.c < b.c) return 1;
  return 0;
}
int pairst[2 * 10000], pairdr[2 * 10000];
char tr[2 * 10000];
int n, k;
int caut(int dist, int x) {
  if (tr[x]) return 0;
  tr[x] = 1;
  if (dist & 1)
    return caut(dist + 1, pairdr[x - n]);
  else {
    int poz;
    poz = ult[x];
    while (poz != -1) {
      if (pairdr[nod[poz] - n] == -1) {
        pairdr[nod[poz] - n] = x;
        pairst[x] = nod[poz] - n;
        return 1;
      }
      poz = next[poz];
    }
    poz = ult[x];
    while (poz != -1) {
      if (caut(dist + 1, nod[poz])) {
        pairdr[nod[poz] - n] = x;
        pairst[x] = nod[poz] - n;
        return 1;
      }
      poz = next[poz];
    }
  }
  return 0;
}
void del(int x) {
  tr[x] = 0;
  int poz;
  poz = ult[x];
  while (poz != -1) {
    if (tr[nod[poz]]) del(nod[poz]);
    poz = next[poz];
  }
}
int main() {
  int m, i, x, y, z, dr = 0;
  scanf("%d%d", &n, &m);
  for (i = 0; i < m; i++) {
    scanf("%d%d%d", &mc[i].a, &mc[i].b, &mc[i].c);
  }
  std::sort(mc, mc + m, cmp);
  int r = 0, pas;
  for (pas = (1 << 29); pas > 0; pas >>= 1) {
    dr = 0;
    memset(pairst, -1, sizeof pairst);
    memset(pairdr, -1, sizeof pairdr);
    memset(ult, -1, sizeof ult);
    for (i = 0; i < m && mc[i].c <= r + pas; i++) {
      x = mc[i].a;
      y = mc[i].b;
      z = mc[i].c;
      x--;
      y--;
      y += n;
      nod[dr] = x;
      next[dr] = ult[y];
      ult[y] = dr;
      dr++;
      nod[dr] = y;
      next[dr] = ult[x];
      ult[x] = dr;
      dr++;
    }
    char aux = 1;
    while (aux) {
      aux = 0;
      for (i = 0; i < n; i++) {
        if (pairst[i] == -1 && caut(0, i)) aux = 1;
      }
      memset(tr, 0, sizeof tr);
    }
    char g = 0;
    for (i = 0; i < n; i++) {
      if (pairst[i] == -1) g = 1;
    }
    if (g) r += pas;
  }
  if (r + 1 <= mc[m - 1].c)
    printf("%d\n", r + 1);
  else
    printf("-1\n");
  return 0;
}
