#include using namespace std; #define F first #define S second #define R cin>> #define ll long long #define ln cout<<'\n' #define in(a) insert(a) #define pb(a) push_back(a) #define pd(a) printf("%.10f\n",a) #define mem(a) memset(a,0,sizeof(a)) #define all(c) (c).begin(),(c).end() #define iter(c) __typeof((c).begin()) #define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--) #define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++) #define rep(i,n) REP(i,0,n) #define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++) ll check(ll n,ll m,ll x,ll y){return x>=0&&x=0&&yvoid pr(const A &a,const B&...b){cout<void PR(A a,ll n){rep(i,n)cout<<(i?" ":"")< P; vector v[211111]; int d[211111],n; P dfs(int x,int t) { d[x]=t;P p=P(d[x],x); rep(i,v[x].size())if(d[v[x][i]]==MAX)p=max(p,dfs(v[x][i],t+1)); return p; } P calc() { fill(d,d+211111,MAX);P p=dfs(0,0); fill(d,d+211111,MAX);P q=dfs(p.S,0); return P(p.S,q.S); } P pp; set s; ll dfs2(int x,int p) { if(x==pp.S) { s.in(x); return 1; } ll f=0; rep(i,v[x].size()) { ll y=v[x][i]; if(y==p) continue; f|=dfs2(y,x); } if(f) s.in(x); return f; } ll a[222222],k=1; void dfs3(ll x,ll p) { a[x]=k++; ll z=-1; rep(i,v[x].size()) { ll y=v[x][i]; if(y==p) continue; if(s.count(y)) { z=y; continue; } dfs3(y,x); k++; } if(z!=-1) { dfs3(z,x); k++; } } int dep[200010]; int par[200010][20]; void dfs(int x,int y,int d){ dep[x]=d; for(int i=0; i=0;i--){ if(par[a][i]!=par[b][i]) a=par[a][i],b=par[b][i]; } return par[a][0]; } void init() { memset(par,0,sizeof(par)); dfs(0,-1,0); for(int i=0; i<19; i++)for(int j=0; j> x >> y; x--,y--; v[x].pb(y); v[y].pb(x); } pp=calc(); dfs2(pp.F,-1); dfs3(pp.F,-1); init(); P b[n]; rep(i,n) b[i]=P(a[i],i); sort(b,b+n); ll ans=0; rep(i,n-1) ans+=D(b[i].S,b[i+1].S); pr(ans); } int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}