#include <bits/stdc++.h>
using namespace std;
vector<int> par, siz;
int n, m;
pair<int, pair<int, int> > qw;
int fiP(int x) {
  if (par[x] == x) return x;
  return par[x] = fiP(par[x]);
}
void uni(int x, int y) {
  int a = fiP(x), b = fiP(y);
  if (siz[a] < siz[b]) swap(a, b);
  par[b] = a, siz[a] += siz[b];
}
bool can(int ans, set<pair<int, pair<int, int> > > &temp) {
  for (int i = 0; i < n; i++) par[i + 1] = i + 1, siz[i + 1] = 1;
  temp.erase(qw);
  temp.insert({ans, {qw.second.first, qw.second.second}});
  int ch = 0;
  for (auto edge : temp) {
    int x = fiP(edge.second.first), y = fiP(edge.second.second);
    if (x == y) continue;
    uni(x, y), ch += edge.first;
    if (siz[fiP(1)] == n) break;
  }
  for (int i = 0; i < n; i++) par[i + 1] = i + 1, siz[i + 1] = 1;
  uni(qw.second.first, qw.second.second);
  temp.erase({ans, {qw.second.first, qw.second.second}});
  int ch1 = ans;
  for (auto edge : temp) {
    int x = fiP(edge.second.first), y = fiP(edge.second.second);
    if (x == y) continue;
    uni(x, y), ch1 += edge.first;
    if (siz[fiP(1)] == n) break;
  }
  temp.insert(qw);
  return ch1 == ch;
}
int main(int argc, char **argv) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  par.resize(n + 1), siz.resize(n + 1);
  set<pair<int, pair<int, int> > > ed;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    ed.insert({c, {a, b}});
    if (!i) qw = {c, {a, b}};
  }
  int ans = 0;
  for (int i = 29; i >= 0; i--) {
    if (can(ans + (1 << i), ed)) ans += 1 << i;
  }
  cout << min(ans, 1000000007 - 7) << endl;
}
