#include <bits/stdc++.h>
int main() {
  int i, a, b, c, n, m, s[1000], temp, j;
  scanf("%d %d", &n, &m);
  for (i = 0; i < m; i++) {
    scanf("%d %d %d", &a, &b, &c);
    s[i] = c;
  }
  for (i = 0; i < m; i++) {
    for (j = 0; j < m - 1; j++) {
      if (s[j + 1] < s[j]) {
        temp = s[j + 1];
        s[j + 1] = s[j];
        s[j] = temp;
      }
    }
  }
  if (s[j] == 1 || s[j] == 2)
    printf("%d\n", s[j] - 1);
  else
    printf("%d\n", s[j] - 2);
  return 0;
}
