#include <bits/stdc++.h>
using namespace std;
long long au, av, aw;
long long findpar(long long x, vector<long long> &par) {
  if (par[x] == x) return x;
  par[x] = findpar(par[x], par);
  return par[x];
}
void add(long long u, long long v, vector<long long> &par) {
  u = findpar(u, par);
  v = findpar(v, par);
  par[u] = v;
}
bool solve(long long n, vector<pair<long long, pair<long long, long long>>> E) {
  vector<long long> par(n + 1);
  for (int i = 1; i <= n; i++) par[i] = i;
  long long tw = E.back().first;
  sort((E).begin(), (E).end());
  for (int i = 0; i < E.size(); i++) {
    int w = E[i].first, u = E[i].second.first, v = E[i].second.second;
    if (findpar(u, par) == findpar(v, par))
      continue;
    else {
      add(u, v, par);
      if (findpar(au, par) == findpar(av, par)) return (w == tw);
    }
  }
  return false;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long n, m;
  cin >> n >> m;
  vector<pair<long long, pair<long long, long long>>> E;
  for (int i = 0; i < m; i++) {
    long long u, v, w;
    cin >> u >> v >> w;
    if (i == 0)
      au = u, av = v, aw = w;
    else {
      E.push_back({w, {u, v}});
    }
  }
  if (m == n - 1)
    cout << 1000000000 << '\n';
  else {
    E.push_back({aw, {au, av}});
    long long low = 0, hi = 1000000000, mid, ans = 0;
    while (low <= hi) {
      mid = (low + hi) / 2;
      E.pop_back();
      E.push_back({mid, {au, av}});
      vector<pair<long long, pair<long long, long long>>> EE = E;
      if (solve(n, EE)) {
        ans = mid;
        low = mid + 1;
      } else
        hi = mid - 1;
    }
    cout << ans << '\n';
  }
  return 0;
}
