#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_5_A"
//#include"../../template/template.hpp"//#include"../../tree/dynamic-rerooting.hpp"usingnamespaceNyaan;structHalfPath{lld;intu;friendHalfPathmax(constHalfPath&lhs,constHalfPath&rhs){if(lhs.d!=rhs.d)returnlhs.d>rhs.d?lhs:rhs;returnlhs.u>rhs.u?lhs:rhs;}};structPath{lld;intu,v;friendPathmax(constPath&lhs,constPath&rhs){if(lhs.d!=rhs.d)returnlhs.d>rhs.d?lhs:rhs;if(lhs.u!=rhs.u)returnlhs.u>rhs.u?lhs:rhs;returnlhs.v>rhs.v?lhs:rhs;}};structL{Pathdia;HalfPathd1,d2;};structH{Pathdia;HalfPathpd,cd;llp_to_c;intp,c;};usingI=pair<ll,int>;Hvertex(constI&xi){auto[x,i]=xi;Hr;r.dia={x,i,i};r.pd=r.cd={x,i};r.p_to_c=x;r.p=r.c=i;returnr;}Hcompress(constH&p,constH&c){Hr;r.dia=max(max(p.dia,c.dia),{p.cd.d+c.pd.d,p.cd.u,c.pd.u});r.pd=max(p.pd,{p.p_to_c+c.pd.d,c.pd.u});r.cd=max(c.cd,{c.p_to_c+p.cd.d,p.cd.u});r.p_to_c=p.p_to_c+c.p_to_c;r.p=p.p,r.c=c.c;returnr;}Lrake(constL&a,constL&b){Lr;r.dia=max(a.dia,b.dia);if(a.d1.d>b.d1.d){r.d1=a.d1;r.d2=max(a.d2,b.d1);}else{r.d1=b.d1;r.d2=max(b.d2,a.d1);}returnr;}Ladd_edge(constH&a){Lr;r.dia=a.dia;r.d1=a.pd;r.d2={0,-1};returnr;}Hadd_vertex(constL&a,constI&xi){auto[x,i]=xi;Hr;r.dia=max(a.dia,{a.d1.d+x+a.d2.d,a.d1.u,a.d2.u});r.pd=r.cd={a.d1.d+x,a.d1.u};r.p_to_c=x;r.p=r.c=i;returnr;}usingDD=TopTree<H,L,I,vertex,compress,rake,add_edge,add_vertex>;voidq(){intN;cin>>N;DDlct;vector<vector<pair<int,int>>>g(N);for(inti=1;i<N;i++){intx,y,z;cin>>x>>y>>z;g[x].emplace_back(y,z);g[y].emplace_back(x,z);}vector<typenameDD::NP>vs(N),es(N);autodfs=[&](autorec,intidx,intpar)->void{vs[idx]=lct.alloc(make_pair(0,idx));for(auto&e:g[idx]){if(e.first==par)continue;rec(rec,e.first,idx);es[e.first]=lct.alloc(make_pair(e.second,-1));lct.link(vs[e.first],es[e.first]);lct.link(es[e.first],vs[idx]);}};dfs(dfs,0,-1);set<longlong>anss;for(inti=0;i<N;i++){longlongd=lct.query(vs[i]).dia.d;anss.emplace(d);}if(anss.size()!=1u)exit(1);cout<<*begin(anss)<<endl;}voidNyaan::solve(){intt=1;// in(t);while(t--)q();}
#line 1 "verify/verify-aoj-grl/aoj-grl-5-a-dynamic-rerooting.test.cpp"
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_5_A"
//#line 2 "template/template.hpp"
usingnamespacestd;// intrinstic#include<immintrin.h>#include<algorithm>
#include<array>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cfenv>
#include<cfloat>
#include<chrono>
#include<cinttypes>
#include<climits>
#include<cmath>
#include<complex>
#include<cstdarg>
#include<cstddef>
#include<cstdint>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<deque>
#include<fstream>
#include<functional>
#include<initializer_list>
#include<iomanip>
#include<ios>
#include<iostream>
#include<istream>
#include<iterator>
#include<limits>
#include<list>
#include<map>
#include<memory>
#include<new>
#include<numeric>
#include<ostream>
#include<queue>
#include<random>
#include<set>
#include<sstream>
#include<stack>
#include<streambuf>
#include<string>
#include<tuple>
#include<type_traits>
#include<typeinfo>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>// utility#line 1 "template/util.hpp"
namespaceNyaan{usingll=longlong;usingi64=longlong;usingu64=unsignedlonglong;usingi128=__int128_t;usingu128=__uint128_t;template<typenameT>usingV=vector<T>;template<typenameT>usingVV=vector<vector<T>>;usingvi=vector<int>;usingvl=vector<longlong>;usingvd=V<double>;usingvs=V<string>;usingvvi=vector<vector<int>>;usingvvl=vector<vector<longlong>>;template<typenameT>usingminpq=priority_queue<T,vector<T>,greater<T>>;template<typenameT,typenameU>structP:pair<T,U>{template<typename...Args>P(Args...args):pair<T,U>(args...){}usingpair<T,U>::first;usingpair<T,U>::second;P&operator+=(constP&r){first+=r.first;second+=r.second;return*this;}P&operator-=(constP&r){first-=r.first;second-=r.second;return*this;}P&operator*=(constP&r){first*=r.first;second*=r.second;return*this;}template<typenameS>P&operator*=(constS&r){first*=r,second*=r;return*this;}Poperator+(constP&r)const{returnP(*this)+=r;}Poperator-(constP&r)const{returnP(*this)-=r;}Poperator*(constP&r)const{returnP(*this)*=r;}template<typenameS>Poperator*(constS&r)const{returnP(*this)*=r;}Poperator-()const{returnP{-first,-second};}};usingpl=P<ll,ll>;usingpi=P<int,int>;usingvp=V<pl>;constexprintinf=1001001001;constexprlonglonginfLL=4004004004004004004LL;template<typenameT>intsz(constT&t){returnt.size();}template<typenameT,typenameU>inlineboolamin(T&x,Uy){return(y<x)?(x=y,true):false;}template<typenameT,typenameU>inlineboolamax(T&x,Uy){return(x<y)?(x=y,true):false;}template<typenameT>inlineTMax(constvector<T>&v){return*max_element(begin(v),end(v));}template<typenameT>inlineTMin(constvector<T>&v){return*min_element(begin(v),end(v));}template<typenameT>inlinelonglongSum(constvector<T>&v){returnaccumulate(begin(v),end(v),0LL);}template<typenameT>intlb(constvector<T>&v,constT&a){returnlower_bound(begin(v),end(v),a)-begin(v);}template<typenameT>intub(constvector<T>&v,constT&a){returnupper_bound(begin(v),end(v),a)-begin(v);}constexprlonglongTEN(intn){longlongret=1,x=10;for(;n;x*=x,n>>=1)ret*=(n&1?x:1);returnret;}template<typenameT,typenameU>pair<T,U>mkp(constT&t,constU&u){returnmake_pair(t,u);}template<typenameT>vector<T>mkrui(constvector<T>&v,boolrev=false){vector<T>ret(v.size()+1);if(rev){for(inti=int(v.size())-1;i>=0;i--)ret[i]=v[i]+ret[i+1];}else{for(inti=0;i<int(v.size());i++)ret[i+1]=ret[i]+v[i];}returnret;};template<typenameT>vector<T>mkuni(constvector<T>&v){vector<T>ret(v);sort(ret.begin(),ret.end());ret.erase(unique(ret.begin(),ret.end()),ret.end());returnret;}template<typenameF>vector<int>mkord(intN,Ff){vector<int>ord(N);iota(begin(ord),end(ord),0);sort(begin(ord),end(ord),f);returnord;}template<typenameT>vector<int>mkinv(vector<T>&v){intmax_val=*max_element(begin(v),end(v));vector<int>inv(max_val+1,-1);for(inti=0;i<(int)v.size();i++)inv[v[i]]=i;returninv;}vector<int>mkiota(intn){vector<int>ret(n);iota(begin(ret),end(ret),0);returnret;}template<typenameT>Tmkrev(constT&v){Tw{v};reverse(begin(w),end(w));returnw;}template<typenameT>boolnxp(T&v){returnnext_permutation(begin(v),end(v));}// 返り値の型は入力の T に依存// i 要素目 : [0, a[i])template<typenameT>vector<vector<T>>product(constvector<T>&a){vector<vector<T>>ret;vector<T>v;autodfs=[&](autorc,inti)->void{if(i==(int)a.size()){ret.push_back(v);return;}for(intj=0;j<a[i];j++)v.push_back(j),rc(rc,i+1),v.pop_back();};dfs(dfs,0);returnret;}// F : function(void(T&)), mod を取る操作// T : 整数型のときはオーバーフローに注意するtemplate<typenameT>TPower(Ta,longlongn,constT&I,constfunction<void(T&)>&f){Tres=I;for(;n;f(a=a*a),n>>=1){if(n&1)f(res=res*a);}returnres;}// T : 整数型のときはオーバーフローに注意するtemplate<typenameT>TPower(Ta,longlongn,constT&I=T{1}){returnPower(a,n,I,function<void(T&)>{[](T&)->void{}});}template<typenameT>TRev(constT&v){Tres=v;reverse(begin(res),end(res));returnres;}template<typenameT>vector<T>Transpose(constvector<T>&v){usingU=typenameT::value_type;if(v.empty())return{};intH=v.size(),W=v[0].size();vectorres(W,T(H,U{}));for(inti=0;i<H;i++){for(intj=0;j<W;j++){res[j][i]=v[i][j];}}returnres;}template<typenameT>vector<T>Rotate(constvector<T>&v,intclockwise=true){usingU=typenameT::value_type;intH=v.size(),W=v[0].size();vectorres(W,T(H,U{}));for(inti=0;i<H;i++){for(intj=0;j<W;j++){if(clockwise){res[W-1-j][i]=v[i][j];}else{res[j][H-1-i]=v[i][j];}}}returnres;}}// namespace Nyaan#line 58 "template/template.hpp"
// bit operation#line 1 "template/bitop.hpp"
namespaceNyaan{__attribute__((target("popcnt")))inlineintpopcnt(constu64&a){return__builtin_popcountll(a);}inlineintlsb(constu64&a){returna?__builtin_ctzll(a):64;}inlineintctz(constu64&a){returna?__builtin_ctzll(a):64;}inlineintmsb(constu64&a){returna?63-__builtin_clzll(a):-1;}template<typenameT>inlineintgbit(constT&a,inti){return(a>>i)&1;}template<typenameT>inlinevoidsbit(T&a,inti,boolb){if(gbit(a,i)!=b)a^=T(1)<<i;}constexprlonglongPW(intn){return1LL<<n;}constexprlonglongMSK(intn){return(1LL<<n)-1;}}// namespace Nyaan#line 61 "template/template.hpp"
// inout#line 1 "template/inout.hpp"
namespaceNyaan{template<typenameT,typenameU>ostream&operator<<(ostream&os,constpair<T,U>&p){os<<p.first<<" "<<p.second;returnos;}template<typenameT,typenameU>istream&operator>>(istream&is,pair<T,U>&p){is>>p.first>>p.second;returnis;}template<typenameT>ostream&operator<<(ostream&os,constvector<T>&v){ints=(int)v.size();for(inti=0;i<s;i++)os<<(i?" ":"")<<v[i];returnos;}template<typenameT>istream&operator>>(istream&is,vector<T>&v){for(auto&x:v)is>>x;returnis;}istream&operator>>(istream&is,__int128_t&x){stringS;is>>S;x=0;intflag=0;for(auto&c:S){if(c=='-'){flag=true;continue;}x*=10;x+=c-'0';}if(flag)x=-x;returnis;}istream&operator>>(istream&is,__uint128_t&x){stringS;is>>S;x=0;for(auto&c:S){x*=10;x+=c-'0';}returnis;}ostream&operator<<(ostream&os,__int128_tx){if(x==0)returnos<<0;if(x<0)os<<'-',x=-x;stringS;while(x)S.push_back('0'+x%10),x/=10;reverse(begin(S),end(S));returnos<<S;}ostream&operator<<(ostream&os,__uint128_tx){if(x==0)returnos<<0;stringS;while(x)S.push_back('0'+x%10),x/=10;reverse(begin(S),end(S));returnos<<S;}voidin(){}template<typenameT,class...U>voidin(T&t,U&...u){cin>>t;in(u...);}voidout(){cout<<"\n";}template<typenameT,class...U,charsep=' '>voidout(constT&t,constU&...u){cout<<t;if(sizeof...(u))cout<<sep;out(u...);}structIoSetupNya{IoSetupNya(){cin.tie(nullptr);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);cerr<<fixed<<setprecision(7);}}iosetupnya;}// namespace Nyaan#line 64 "template/template.hpp"
// debug#line 1 "template/debug.hpp"
namespaceDebugImpl{template<typenameU,typename=void>structis_specialize:false_type{};template<typenameU>structis_specialize<U,typenameconditional<false,typenameU::iterator,void>::type>:true_type{};template<typenameU>structis_specialize<U,typenameconditional<false,decltype(U::first),void>::type>:true_type{};template<typenameU>structis_specialize<U,enable_if_t<is_integral<U>::value,void>>:true_type{};voiddump(constchar&t){cerr<<t;}voiddump(conststring&t){cerr<<t;}voiddump(constbool&t){cerr<<(t?"true":"false");}voiddump(__int128_tt){if(t==0)cerr<<0;if(t<0)cerr<<'-',t=-t;stringS;while(t)S.push_back('0'+t%10),t/=10;reverse(begin(S),end(S));cerr<<S;}voiddump(__uint128_tt){if(t==0)cerr<<0;stringS;while(t)S.push_back('0'+t%10),t/=10;reverse(begin(S),end(S));cerr<<S;}template<typenameU,enable_if_t<!is_specialize<U>::value,nullptr_t>=nullptr>voiddump(constU&t){cerr<<t;}template<typenameT>voiddump(constT&t,enable_if_t<is_integral<T>::value>*=nullptr){stringres;if(t==Nyaan::inf)res="inf";ifconstexpr(is_signed<T>::value){if(t==-Nyaan::inf)res="-inf";}ifconstexpr(sizeof(T)==8){if(t==Nyaan::infLL)res="inf";ifconstexpr(is_signed<T>::value){if(t==-Nyaan::infLL)res="-inf";}}if(res.empty())res=to_string(t);cerr<<res;}template<typenameT,typenameU>voiddump(constpair<T,U>&);template<typenameT>voiddump(constpair<T*,int>&);template<typenameT>voiddump(constT&t,enable_if_t<!is_void<typenameT::iterator>::value>*=nullptr){cerr<<"[ ";for(autoit=t.begin();it!=t.end();){dump(*it);cerr<<(++it==t.end()?"":", ");}cerr<<" ]";}template<typenameT,typenameU>voiddump(constpair<T,U>&t){cerr<<"( ";dump(t.first);cerr<<", ";dump(t.second);cerr<<" )";}template<typenameT>voiddump(constpair<T*,int>&t){cerr<<"[ ";for(inti=0;i<t.second;i++){dump(t.first[i]);cerr<<(i==t.second-1?"":", ");}cerr<<" ]";}voidtrace(){cerr<<endl;}template<typenameHead,typename...Tail>voidtrace(Head&&head,Tail&&...tail){cerr<<" ";dump(head);if(sizeof...(tail)!=0)cerr<<",";trace(std::forward<Tail>(tail)...);}}// namespace DebugImpl#ifdef NyaanDebug
#define trc(...) \
do { \
cerr << "## " << #__VA_ARGS__ << " = "; \
DebugImpl::trace(__VA_ARGS__); \
} while (0)
#else
#define trc(...) (void(0))
#endif
#ifdef NyaanLocal
#define trc2(...) \
do { \
cerr << "## " << #__VA_ARGS__ << " = "; \
DebugImpl::trace(__VA_ARGS__); \
} while (0)
#else
#define trc2(...) (void(0))
#endif
#line 67 "template/template.hpp"
// macro#line 1 "template/macro.hpp"
#define each(x, v) for (auto&& x : v)
#define each2(x, y, v) for (auto&& [x, y] : v)
#define all(v) (v).begin(), (v).end()
#define rep(i, N) for (long long i = 0; i < (long long)(N); i++)
#define repr(i, N) for (long long i = (long long)(N)-1; i >= 0; i--)
#define rep1(i, N) for (long long i = 1; i <= (long long)(N); i++)
#define repr1(i, N) for (long long i = (N); (long long)(i) > 0; i--)
#define reg(i, a, b) for (long long i = (a); i < (b); i++)
#define regr(i, a, b) for (long long i = (b)-1; i >= (a); i--)
#define fi first
#define se second
#define ini(...) \
int __VA_ARGS__; \
in(__VA_ARGS__)
#define inl(...) \
long long __VA_ARGS__; \
in(__VA_ARGS__)
#define ins(...) \
string __VA_ARGS__; \
in(__VA_ARGS__)
#define in2(s, t) \
for (int i = 0; i < (int)s.size(); i++) { \
in(s[i], t[i]); \
}
#define in3(s, t, u) \
for (int i = 0; i < (int)s.size(); i++) { \
in(s[i], t[i], u[i]); \
}
#define in4(s, t, u, v) \
for (int i = 0; i < (int)s.size(); i++) { \
in(s[i], t[i], u[i], v[i]); \
}
#define die(...) \
do { \
Nyaan::out(__VA_ARGS__); \
return; \
} while (0)
#line 70 "template/template.hpp"
namespaceNyaan{voidsolve();}intmain(){Nyaan::solve();}#line 4 "verify/verify-aoj-grl/aoj-grl-5-a-dynamic-rerooting.test.cpp"
//#line 2 "tree/dynamic-rerooting.hpp"
namespaceDynamicRerootingImpl{template<typenamePoint,Point(*rake)(constPoint&,constPoint&)>structSplayTreeforDashedEdge{structNode{Node*l,*r,*p;Pointkey,sum;explicitNode(constPoint&_key):l(nullptr),r(nullptr),p(nullptr),key(_key),sum(_key){}};SplayTreeforDashedEdge(){}usingNP=Node*;voidrotr(NPt){NPx=t->p,y=x->p;if((x->l=t->r))t->r->p=x;t->r=x,x->p=t;update(x),update(t);if((t->p=y)){if(y->l==x)y->l=t;if(y->r==x)y->r=t;}}voidrotl(NPt){NPx=t->p,y=x->p;if((x->r=t->l))t->l->p=x;t->l=x,x->p=t;update(x),update(t);if((t->p=y)){if(y->l==x)y->l=t;if(y->r==x)y->r=t;}}voidupdate(NPt){t->sum=t->key;if(t->l)t->sum=rake(t->sum,t->l->sum);if(t->r)t->sum=rake(t->sum,t->r->sum);}NPget_right(NPt){while(t->r)t=t->r;returnt;}NPalloc(constPoint&v){autot=newNode(v);update(t);returnt;}voidsplay(NPt){while(t->p){NPq=t->p;if(!q->p){if(q->l==t)rotr(t);elserotl(t);}else{NPr=q->p;if(r->l==q){if(q->l==t)rotr(q),rotr(t);elserotl(t),rotr(t);}else{if(q->r==t)rotl(q),rotl(t);elserotr(t),rotl(t);}}}}NPinsert(NPt,constPoint&v){if(nott){t=alloc(v);returnt;}else{NPcur=get_right(t),z=alloc(v);splay(cur);z->p=cur;cur->r=z;update(cur);splay(z);returnz;}}NPerase(NPt){splay(t);NPx=t->l,y=t->r;deletet;if(notx){t=y;if(t)t->p=nullptr;}elseif(noty){t=x;t->p=nullptr;}else{x->p=nullptr;t=get_right(x);splay(t);t->r=y;y->p=t;update(t);}returnt;}};template<typenamePath,typenamePoint,typenameInfo,Path(*vertex)(constInfo&),Path(*compress)(constPath&,constPath&),Point(*rake)(constPoint&,constPoint&),Point(*add_edge)(constPath&),Path(*add_vertex)(constPoint&,constInfo&)>structTopTree{private:structNode{Node*l,*r,*p;Infoinfo;Pathkey,sum,mus;typenameSplayTreeforDashedEdge<Point,rake>::Node*light,*belong;boolrev;boolis_root()const{returnnotpor(p->l!=thisandp->r!=this);}explicitNode(constInfo_info):l(nullptr),r(nullptr),p(nullptr),info(_info),light(nullptr),belong(nullptr),rev(false){}};public:usingNP=Node*;SplayTreeforDashedEdge<Point,rake>splay_tree;private:voidtoggle(NPt){swap(t->l,t->r);swap(t->sum,t->mus);t->rev^=true;}voidrotr(NPt){NPx=t->p,y=x->p;push(x),push(t);if((x->l=t->r))t->r->p=x;t->r=x,x->p=t;update(x),update(t);if((t->p=y)){if(y->l==x)y->l=t;if(y->r==x)y->r=t;}}voidrotl(NPt){NPx=t->p,y=x->p;push(x),push(t);if((x->r=t->l))t->l->p=x;t->l=x,x->p=t;update(x),update(t);if((t->p=y)){if(y->l==x)y->l=t;if(y->r==x)y->r=t;}}public:TopTree():splay_tree{}{}voidpush(NPt){if(t->rev){if(t->l)toggle(t->l);if(t->r)toggle(t->r);t->rev=false;}}voidpush_rev(NPt){if(t->rev){if(t->l)toggle(t->l);if(t->r)toggle(t->r);t->rev=false;}}voidupdate(NPt){Pathkey=t->light?add_vertex(t->light->sum,t->info):vertex(t->info);Pathsum=key,mus=key;if(t->l)sum=compress(t->l->sum,sum),mus=compress(mus,t->l->mus);if(t->r)sum=compress(sum,t->r->sum),mus=compress(t->r->mus,mus);t->key=key,t->sum=sum,t->mus=mus;}voidsplay(NPt){push(t);{NProt=t;while(notrot->is_root())rot=rot->p;t->belong=rot->belong;if(t!=rot)rot->belong=nullptr;}while(nott->is_root()){NPq=t->p;if(q->is_root()){push_rev(q),push_rev(t);if(q->l==t)rotr(t);elserotl(t);}else{NPr=q->p;push_rev(r),push_rev(q),push_rev(t);if(r->l==q){if(q->l==t)rotr(q),rotr(t);elserotl(t),rotr(t);}else{if(q->r==t)rotl(q),rotl(t);elserotr(t),rotl(t);}}}}NPexpose(NPt){NPrp=nullptr;for(NPcur=t;cur;cur=cur->p){splay(cur);if(cur->r){cur->light=splay_tree.insert(cur->light,add_edge(cur->r->sum));cur->r->belong=cur->light;}cur->r=rp;if(cur->r){splay_tree.splay(cur->r->belong);push(cur->r);cur->light=splay_tree.erase(cur->r->belong);}update(cur);rp=cur;}splay(t);returnrp;}voidlink(NPchild,NPparent){expose(parent);expose(child);child->p=parent;parent->r=child;update(parent);}voidcut(NPchild){expose(child);NPparent=child->l;child->l=nullptr;parent->p=nullptr;update(child);}voidevert(NPt){expose(t);toggle(t);push(t);}NPalloc(constInfo&info){NPt=newNode(info);update(t);returnt;}boolis_connected(NPu,NPv){expose(u),expose(v);returnu==voru->p;}NPlca(NPu,NPv){if(notis_connected(u,v))returnnullptr;expose(u);returnexpose(v);}voidset_key(NPt,constInfo&v){expose(t);t->info=v;update(t);}// u を根とする sumPathquery(NPu){evert(u);returnu->sum;}// root を根, u を部分木の根とする sumPathquery_subtree(NProot,NPu){evert(root);expose(u);NPl=u->l;u->l=nullptr;update(u);autoret=u->sum;u->l=l;update(u);returnret;}};template<typenamePath,typenamePoint,typenameInfo,Path(*vertex)(constInfo&),Path(*compress)(constPath&,constPath&),Point(*rake)(constPoint&,constPoint&),Point(*Add_edge)(constPath&),Path(*add_vertex)(constPoint&,constInfo&)>structDynamicRerooting{intn;TopTree<Path,Point,Info,vertex,compress,rake,Add_edge,add_vertex>tt;usingNP=typenamedecltype(tt)::NP;vector<NP>vs;DynamicRerooting(int_n,constvector<Info>&info):n(_n),vs(n){for(inti=0;i<n;i++)vs[i]=tt.alloc(info[i]);}// u-v 間に辺を追加voidadd_edge(intu,intv){tt.evert(vs[u]);tt.link(vs[u],vs[v]);}// u-v 間の辺を削除voiddel_edge(intu,intv){tt.evert(vs[u]);tt.cut(vs[v]);}// 頂点 u の情報を取得Infoget_info(intu){returnvs[u]->info;}// 頂点 u の情報を設定voidset_info(intu,constInfo&info){tt.set_key(vs[u],info);}// 頂点 u を根とするクエリPathquery(intu){returntt.query(vs[u]);}// 頂点 root を根, 頂点 u を部分木の根とするクエリPathquery_subtree(introot,intu){returntt.query_subtree(vs[root],vs[u]);}};}// namespace DynamicRerootingImplusingDynamicRerootingImpl::DynamicRerooting;usingDynamicRerootingImpl::TopTree;/*
struct Path {
};
struct Point {
};
struct Info {
};
Path vertex(const Info &i) {
}
Path compress(const Path &p, const Path &c) {
}
Point rake(const Point &a, const Point &b) {
}
Point add_edge(const Path &a) {
}
Path add_vertex(const Point &a, const Info &i) {
}
using DR = DynamicRerooting<Path, Point, Info, vertex, compress, rake, add_edge,
add_vertex>;
*/#line 6 "verify/verify-aoj-grl/aoj-grl-5-a-dynamic-rerooting.test.cpp"
usingnamespaceNyaan;structHalfPath{lld;intu;friendHalfPathmax(constHalfPath&lhs,constHalfPath&rhs){if(lhs.d!=rhs.d)returnlhs.d>rhs.d?lhs:rhs;returnlhs.u>rhs.u?lhs:rhs;}};structPath{lld;intu,v;friendPathmax(constPath&lhs,constPath&rhs){if(lhs.d!=rhs.d)returnlhs.d>rhs.d?lhs:rhs;if(lhs.u!=rhs.u)returnlhs.u>rhs.u?lhs:rhs;returnlhs.v>rhs.v?lhs:rhs;}};structL{Pathdia;HalfPathd1,d2;};structH{Pathdia;HalfPathpd,cd;llp_to_c;intp,c;};usingI=pair<ll,int>;Hvertex(constI&xi){auto[x,i]=xi;Hr;r.dia={x,i,i};r.pd=r.cd={x,i};r.p_to_c=x;r.p=r.c=i;returnr;}Hcompress(constH&p,constH&c){Hr;r.dia=max(max(p.dia,c.dia),{p.cd.d+c.pd.d,p.cd.u,c.pd.u});r.pd=max(p.pd,{p.p_to_c+c.pd.d,c.pd.u});r.cd=max(c.cd,{c.p_to_c+p.cd.d,p.cd.u});r.p_to_c=p.p_to_c+c.p_to_c;r.p=p.p,r.c=c.c;returnr;}Lrake(constL&a,constL&b){Lr;r.dia=max(a.dia,b.dia);if(a.d1.d>b.d1.d){r.d1=a.d1;r.d2=max(a.d2,b.d1);}else{r.d1=b.d1;r.d2=max(b.d2,a.d1);}returnr;}Ladd_edge(constH&a){Lr;r.dia=a.dia;r.d1=a.pd;r.d2={0,-1};returnr;}Hadd_vertex(constL&a,constI&xi){auto[x,i]=xi;Hr;r.dia=max(a.dia,{a.d1.d+x+a.d2.d,a.d1.u,a.d2.u});r.pd=r.cd={a.d1.d+x,a.d1.u};r.p_to_c=x;r.p=r.c=i;returnr;}usingDD=TopTree<H,L,I,vertex,compress,rake,add_edge,add_vertex>;voidq(){intN;cin>>N;DDlct;vector<vector<pair<int,int>>>g(N);for(inti=1;i<N;i++){intx,y,z;cin>>x>>y>>z;g[x].emplace_back(y,z);g[y].emplace_back(x,z);}vector<typenameDD::NP>vs(N),es(N);autodfs=[&](autorec,intidx,intpar)->void{vs[idx]=lct.alloc(make_pair(0,idx));for(auto&e:g[idx]){if(e.first==par)continue;rec(rec,e.first,idx);es[e.first]=lct.alloc(make_pair(e.second,-1));lct.link(vs[e.first],es[e.first]);lct.link(es[e.first],vs[idx]);}};dfs(dfs,0,-1);set<longlong>anss;for(inti=0;i<N;i++){longlongd=lct.query(vs[i]).dia.d;anss.emplace(d);}if(anss.size()!=1u)exit(1);cout<<*begin(anss)<<endl;}voidNyaan::solve(){intt=1;// in(t);while(t--)q();}