#include using namespace std; const int SQR = 400; const int MAXN = 100 * 1000 + 5; vector G[MAXN]; int main() { //ifstream cin("d.in"); int n; cin >> n; vector a(n - 1, 0), b(n - 1, 0), cost(n - 1, 0); for(int i = 0; i < n - 1; ++i) { cin >> a[i] >> b[i]; a[i]--; b[i]--; cost[i] = a[i] + b[i]; } vector p(n - 1, 0); for(int i = 0; i < n; ++i) p[i] = i; sort(p.begin(), p.end(), [&](int x, int y) { return cost[x] < cost[y]; }); long long ans = 0; vector dad(n, 0); vector> cnt(n); for(int i = 0; i < n; ++i) { dad[i] = i; cnt[i] = vector (SQR + 5, 0); cnt[i][i / SQR]++; G[i].push_back(i); } for(int ind = 0; ind < n - 1; ++ind) { int idx = p[ind]; int fa = dad[a[idx]]; int fb = dad[b[idx]]; int last = cost[idx]; if(fa != fb) { int szA = G[fa].size(); int szB = G[fb].size(); if(szA > szB) { swap(fa, fb); } for(auto temp : G[fa]) { G[fb].push_back(temp); int atLeast = last - temp; int nxt = atLeast / SQR; if(atLeast % SQR) nxt++; int orig = nxt; while(nxt < SQR) { ans += cnt[fb][nxt]; nxt++; } for(int who = atLeast; who < orig * SQR and who < n; ++who) if(dad[who] == fb) { ans++; } } for(auto temp : G[fa]) { dad[temp] = fb; cnt[fb][temp / SQR]++; } G[fa].clear(); } } //ans += n - 1; cout << ans << "\n"; }