#include <bits/stdc++.h>
using namespace std;
long long dsu_get(long long v, vector<long long>& p) {
  if (v == p[v]) {
    return v;
  } else {
    p[v] = dsu_get(p[v], p);
    return p[v];
  }
}
void dsu_unite(long long a, long long b, vector<long long>& p) {
  a = dsu_get(a, p);
  b = dsu_get(b, p);
  if (rand() & 1) swap(a, b);
  if (a != b) p[a] = b;
}
long long kruskal(long long& n, long long& m,
                  vector<pair<long long, pair<long long, long long>>> g) {
  long long cost = 0;
  vector<long long> p(n);
  for (long long i = 0; i < n; ++i) {
    p[i] = i;
  }
  sort(g.begin(), g.end());
  for (long long i = 0; i < m; ++i) {
    long long a = g[i].second.first, b = g[i].second.second, l = g[i].first;
    if (dsu_get(a, p) != dsu_get(b, p)) {
      cost += l;
      dsu_unite(a, b, p);
    }
  }
  return cost;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  long long n, m;
  cin >> n >> m;
  vector<pair<long long, pair<long long, long long>>> g(m, {0, {0, 0}});
  for (long long i = 0; i < m; ++i) {
    cin >> g[i].second.first >> g[i].second.second >> g[i].first;
    --g[i].second.first;
    --g[i].second.second;
  }
  g[0].first = 1LL << 35;
  long long cost1 = kruskal(n, m, g);
  g[0].first = 0;
  long long cost2 = kruskal(n, m, g);
  if (cost1 - cost2 <= 1000000000) {
    cout << cost1 - cost2;
  } else {
    cout << 1000000000;
  }
  return 0;
}
