#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
class UnionFind {
 private:
  vector<int> p, rank, setSize;
  int numSets;

 public:
  UnionFind(int N) {
    setSize.assign(N, 1);
    numSets = N;
    rank.assign(N, 0);
    p.assign(N, 0);
    for (int i = 0; i < N; i++) p[i] = i;
  }
  int findSet(int i) { return (p[i] == i) ? i : (p[i] = findSet(p[i])); }
  bool isSameSet(int i, int j) { return findSet(i) == findSet(j); }
  void unionSet(int i, int j) {
    if (!isSameSet(i, j)) {
      numSets--;
      int x = findSet(i), y = findSet(j);
      if (rank[x] > rank[y]) {
        p[y] = x;
        setSize[x] += setSize[y];
      } else {
        p[x] = y;
        setSize[y] += setSize[x];
        if (rank[x] == rank[y]) rank[y]++;
      }
    }
  }
  int numDisjointSets() { return numSets; }
  int sizeOfSet(int i) { return setSize[findSet(i)]; }
};
vector<vector<pair<int, int> > > AdjList;
vector<int> taken;
priority_queue<pair<int, int> > pq;
void process(int vtx) {
  taken[vtx] = 1;
  for (int j = 0; j < (int)AdjList[vtx].size(); j++) {
    pair<int, int> v = AdjList[vtx][j];
    if (!taken[v.first]) pq.push(pair<int, int>(-v.second, -v.first));
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int V, E, u, v, w;
  scanf("%d %d", &V, &E);
  AdjList.assign(V, vector<pair<int, int> >());
  vector<pair<int, pair<int, int> > > EdgeList, EdgeList2;
  int wt, f, s;
  bool flag = false;
  for (int i = 0; i < E; i++) {
    scanf("%d %d %d", &u, &v, &w);
    u--;
    v--;
    EdgeList.push_back(make_pair(w, pair<int, int>(u, v)));
    if (flag)
      EdgeList2.push_back(make_pair(w, pair<int, int>(u, v)));
    else {
      flag = true;
      wt = w;
      f = u;
      s = v;
    }
    AdjList[u].push_back(pair<int, int>(v, w));
    AdjList[v].push_back(pair<int, int>(u, w));
  }
  sort(EdgeList.begin(), EdgeList.end());
  sort(EdgeList2.begin(), EdgeList2.end());
  long long mst_cost = wt;
  UnionFind UF(V);
  UF.unionSet(f, s);
  for (int i = 0; i < E; i++) {
    pair<int, pair<int, int> > front = EdgeList[i];
    if (!UF.isSameSet(front.second.first, front.second.second)) {
      mst_cost += (long long)front.first;
      UF.unionSet(front.second.first, front.second.second);
    }
  }
  long long mst_cost_2 = 0;
  UnionFind UF2(V);
  for (int i = 0; i < E - 1; i++) {
    pair<int, pair<int, int> > front = EdgeList2[i];
    if (!UF2.isSameSet(front.second.first, front.second.second)) {
      mst_cost_2 += (long long)front.first;
      UF2.unionSet(front.second.first, front.second.second);
    }
  }
  if (!UF2.isSameSet(f, s)) {
    cout << "1000000000";
    return 0;
  }
  if (mst_cost > mst_cost_2) {
    long long res = wt;
    res = res - mst_cost + mst_cost_2;
    cout << res;
  } else {
    long long res = wt;
    res = res - mst_cost + mst_cost_2;
    res = min(res, (long long)1000000000);
    cout << res;
  }
  return 0;
}
