#include #define rep(i,n) for(int i=0;i<(int)(n);i++) #define rep1(i,n) for(int i=1;i<=(int)(n);i++) #define all(c) c.begin(),c.end() #define pb push_back #define fs first #define sc second #define chmin(x,y) x=min(x,y) #define chmax(x,y) x=max(x,y) using namespace std; template ostream& operator<<(ostream& o,const pair &p){ return o<<"("< ostream& operator<<(ostream& o,const vector &vc){ o<<"{"; for(const T& v:vc) o< using V = vector; template using VV = vector>; constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); } #ifdef LOCAL #define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl #else #define show(x) true #endif int main(){ cin.tie(0); ios::sync_with_stdio(false); //DON'T USE scanf/printf/puts !! cout << fixed << setprecision(20); int N; cin >> N; V d(N); VV G(N); rep(i,N-1){ int x,y; cin >> x >> y; x--, y--; d[x]++,d[y]++; } rep(i,N-1){ int x,y; cin >> x >> y; x--, y--; G[x].pb(y);G[y].pb(x); } V depth(N); int inf = 1e9; function(int,int)> dfs = [&](int v,int p){ //erase_num,odd depth[v] = p == -1 ? 0 : depth[p] + 1; VV cur(3,V(2,inf)); cur[0][0] = 0; for(int u: G[v]) if(u != p){ auto d = dfs(u,v); VV nxt(3,V(2,inf)); rep(i,3) rep(j,2) if(cur[i][j] != inf){ rep(k,3) rep(l,2) if(d[k][l] != inf){ if(i+k > 2) continue; int add = (j+l==2) ? -2*depth[v] : 0; chmin(nxt[i+k][(j+l)%2],cur[i][j]+d[k][l]+add); } } cur = nxt; } if(d[v]%2){ VV nxt(3,V(2,inf)); rep(i,3) rep(j,2) if(cur[i][j] != inf){ if(i<2){ //erase v chmin(nxt[i+1][j],cur[i][j]); } if(j == 0){ chmin(nxt[i][1],cur[i][j]+depth[v]); }else{ chmin(nxt[i][0],cur[i][j]-depth[v]); } } cur = nxt; } return cur; }; // rep(r,N){ // show(dfs(r,-1)); // } auto f = dfs(0,-1); cout << N-1 + min(f[2][0],f[0][0]) << endl; }