//#pragma GCC optimize("Ofast") //#pragma GCC target("avx") //#undef LOCAL #include using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); } template using V = vector; template using VV = V>; #ifdef LOCAL struct PrettyOS { ostream& os; bool first; template auto operator<<(T&& x) { if (!first) os << ", "; first = false; os << x; return *this; } }; template void dbg0(T&&... t) { (PrettyOS{cerr, true} << ... << t); } #define dbg(...) \ do { \ cerr << __LINE__ << " : " << #__VA_ARGS__ << " = "; \ dbg0(__VA_ARGS__); \ cerr << endl; \ } while (false); #else #define dbg(...) #endif template ostream& operator<<(ostream& os, const pair& p) { return os << "P(" << p.first << ", " << p.second << ")"; } template ostream& operator<<(ostream& os, const V& v) { os << "["; for (auto d : v) os << d << ", "; return os << "]"; } VV g; V dif, odd, sz; void dfs(int p, int b) { for (int d: g[p]) { if (d == b) continue; dfs(d, p); sz[p] ^= sz[d]; } } int ma = 0; int dfs2(int p, int b) { V top; top.push_back(-TEN(8)); top.push_back(-TEN(8)); if (odd[p]) top.push_back(dif[p]); for (int d: g[p]) { if (d == b) continue; dif[d] = dif[p]; if (sz[d]) dif[d]++; else dif[d]--; top.push_back(dfs2(d, p)); } sort(top.begin(), top.end(), greater()); ma = max(ma, top[0] + top[1] - 2 * dif[p]); return top[0]; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); int n; cin >> n; g = VV(n); for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; a--; b--; g[a].push_back(b); g[b].push_back(a); } V deg(n); for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; a--; b--; deg[a] = 1 - deg[a]; deg[b] = 1 - deg[b]; } odd = V(n); sz = V(n); for (int i = 0; i < n; i++) { if (deg[i] % 2) { odd[i] = sz[i] = 1; } } dfs(0, -1); dbg(odd, sz); assert(sz[0] == 0); int ans = n - 1; for (int i = 0; i < n; i++) { if (sz[i]) ans++; } dif = V(n); dfs2(0, -1); dbg(ans, ma); cout << ans - ma << endl; return 0; }