#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 2;
const long long inf = 1e9 + 7;
long long max1[N], max2[N], best1[N], best2[N], dis1[N], dis2[N], rmq1[N][21],
    rmq2[N][21], wow[N], pos[N], size[N], ver1, ver2, now = 0, par[N], val;
bool used[N];
vector<pair<long long, long long> > adj[N];
void dfs(long long x, long long p) {
  long long cac = 0, cac1 = 0;
  for (long long i = 0; i < adj[x].size(); i++) {
    if (adj[x][i].first != p) {
      if (used[adj[x][i].first]) {
        ver1 = x;
        ver2 = adj[x][i].first;
        val = adj[x][i].second;
      } else {
        used[adj[x][i].first] = true;
        dfs(adj[x][i].first, x);
        max1[x] = max(max1[x], max1[adj[x][i].first]);
        if (best1[adj[x][i].first] + adj[x][i].second >= cac) {
          cac1 = cac;
          cac = best1[adj[x][i].first] + adj[x][i].second;
        } else {
          if (best1[adj[x][i].first] + adj[x][i].second > cac1) {
            cac1 = best1[adj[x][i].first] + adj[x][i].second;
          }
        }
      }
    }
  }
  if (cac + cac1 >= max1[x]) {
    max1[x] = cac + cac1;
  }
  best1[x] = cac;
}
void dfs1(long long x, long long p) {
  now++;
  pos[x] = now;
  wow[now] = x;
  size[x] = 1;
  max1[x] = 0;
  best1[x] = 0;
  long long cac = 0, cac1 = 0;
  for (long long i = 0; i < adj[x].size(); i++) {
    if (adj[x][i].first != p) {
      if (x == ver1 && adj[x][i].first == ver2) {
        continue;
      }
      if (x == ver2 && adj[x][i].first == ver1) {
        continue;
      }
      dis1[adj[x][i].first] = dis1[x] + adj[x][i].second;
      dfs1(adj[x][i].first, x);
      size[x] += size[adj[x][i].first];
      par[adj[x][i].first] = x;
      max1[x] = max(max1[x], max1[adj[x][i].first]);
      if (best1[adj[x][i].first] + adj[x][i].second >= cac) {
        cac1 = cac;
        cac = best1[adj[x][i].first] + adj[x][i].second;
      } else {
        if (best1[adj[x][i].first] + adj[x][i].second > cac1) {
          cac1 = best1[adj[x][i].first] + adj[x][i].second;
        }
      }
    }
  }
  max1[x] = max(max1[x], cac + cac1);
  best1[x] = cac;
}
void dfs2(long long x, long long p) {
  long long cac = 0, cac1 = 0;
  for (long long i = 0; i < adj[x].size(); i++) {
    if (adj[x][i].first != p) {
      if (x == ver1 && adj[x][i].first == ver2) {
        continue;
      }
      if (x == ver2 && adj[x][i].first == ver1) {
        continue;
      }
      dis2[adj[x][i].first] = dis2[x] + adj[x][i].second;
      dfs2(adj[x][i].first, x);
      max2[x] = max(max2[x], max2[adj[x][i].first]);
      if (best2[adj[x][i].first] + adj[x][i].second >= cac) {
        cac1 = cac;
        cac = best2[adj[x][i].first] + adj[x][i].second;
      } else {
        if (best2[adj[x][i].first] + adj[x][i].second > cac1) {
          cac1 = best2[adj[x][i].first] + adj[x][i].second;
        }
      }
    }
  }
  max2[x] = max(max2[x], cac + cac1);
  best2[x] = cac;
}
long long getmax1(long long x, long long y) {
  long long z = log2(y - x + 1);
  return max(rmq1[x][z], rmq1[y - (1 << z) + 1][z]);
}
long long getmax2(long long x, long long y) {
  if (x > y) {
    return -inf;
  }
  long long z = log2(y - x + 1);
  return max(rmq2[x][z], rmq2[y - (1 << z) + 1][z]);
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long n, i, j, k, l, ans;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> j >> k >> l;
    adj[j].push_back({k, l});
    adj[k].push_back({j, l});
  }
  used[1] = true;
  dfs(1, 1);
  ans = max1[1];
  dfs1(ver1, ver1);
  dfs2(ver2, ver2);
  for (i = 1; i <= n; i++) {
    rmq1[i][0] = dis2[wow[i]];
    rmq2[i][0] = dis1[wow[i]];
  }
  for (i = 1; (1 << i) <= n; i++) {
    for (j = 1; j <= n - (1 << i) + 1; j++) {
      rmq1[j][i] = max(rmq1[j][i - 1], rmq1[j + (1 << (i - 1))][i - 1]);
      rmq2[j][i] = max(rmq2[j][i - 1], rmq2[j + (1 << (i - 1))][i - 1]);
    }
  }
  j = ver2;
  while (j != ver1) {
    k = par[j];
    ans = min(
        ans, max(max(max1[j], max2[k]),
                 getmax1(pos[j], pos[j] + size[j] - 1) +
                     max(getmax2(1, pos[j] - 1), getmax2(pos[j] + size[j], n)) +
                     val));
    j = k;
  }
  cout << ans;
}
