/** author: shobonvip created: 2025.06.27 21:35:34 **/ #include using namespace std; //* ATCODER #include using namespace atcoder; typedef modint998244353 mint; //*/ /* BOOST MULTIPRECISION #include using namespace boost::multiprecision; //*/ typedef long long ll; #define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++) #define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--) #define all(v) v.begin(), v.end() template bool chmin(T &a, const T &b) { if (a <= b) return false; a = b; return true; } template bool chmax(T &a, const T &b) { if (a >= b) return false; a = b; return true; } template T max(vector &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]); return ret; } template T min(vector &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]); return ret; } template T sum(vector &a){ T ret = 0; for (int i=0; i<(int)a.size(); i++) ret += a[i]; return ret; } typedef unsigned long long ull; int f1[200001],f2[200001],g1[400001],g2[400001]; ull d1[200001],d2[200001],ans=0; int mada[999999]; bool seen[999999]; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector u(n-1), v(n-1), x(n-1), y(n-1); vector ikeru1(n, vector(0)); vector ikeru2(n, vector(0)); rep(i,0,n-1) { cin >> u[i] >> v[i]; u[i]--; v[i]--; ikeru1[u[i]].push_back(v[i]); ikeru1[v[i]].push_back(u[i]); } rep(i,0,n-1) { cin >> x[i] >> y[i]; x[i]--; y[i]--; ikeru2[x[i]].push_back(y[i]); ikeru2[y[i]].push_back(x[i]); } { int piv = 0; rep(i,0,n) { for (int x: ikeru1[i]) { g1[piv++] = x; } f1[i+1] = piv; } } { int piv = 0; rep(i,0,n) { for (int x: ikeru2[i]) { g2[piv++] = x; } f2[i+1] = piv; } } rep(st,0,n) { { fill(seen,seen+n,false); int piv = 1; mada[0] = st; seen[st] = 1; d1[st] = 0; while (piv > 0) { int i = mada[--piv]; rep(x,f1[i],f1[i+1]){ int j = g1[x]; if (!seen[j]) { d1[j] = d1[i] + 1; mada[piv++] = j; seen[j] = 1; } } } } { fill(seen,seen+n,false); int piv = 1; mada[0] = st; seen[st] = 1; d2[st] = 0; while (piv > 0) { int i = mada[--piv]; rep(x,f2[i],f2[i+1]){ int j = g2[x]; if (!seen[j]) { d2[j] = d2[i] + 1; mada[piv++] = j; seen[j] = 1; } } } } rep(i,st+1,n){ ans += d1[i] * d2[i]; } } ans *= 2; cout << ans << endl; }