using System; using System.Collections.Generic; class Program { Scanner cin; int N; List[] g; int[] sub; int[,] dp; void rec(int idx, int par) { int[] dp2 = { 0, 0, 0 }; foreach(var to in g[idx]) { if (to == par) continue; rec(to, idx); sub[idx] += sub[to]; int[] dp3 = { 10101010, 10101010, 10101010 }; for (int i = 0; i < 3; i++) { for(int j = 0; i + j < 3; j++) { dp3[i + j] = Math.Min(dp3[i + j], dp2[i] + dp[to, j] + (sub[to] + j) % 2); } } for (int i = 0; i < 3; i++) { dp2[i] = dp3[i]; } } for(int i = 0; i < 3; i++) { dp[idx, i] = dp2[i]; } } void myon() { cin = new Scanner(); N = cin.nextInt(); g = new List[N]; for(int i = 0; i < N; i++) { g[i] = new List(); } for(int i = 1; i < N; i++) { int a = cin.nextInt() - 1; int b = cin.nextInt() - 1; g[a].Add(b); g[b].Add(a); } sub = new int[N]; for(int i = 1; i < N; i++) { int a = cin.nextInt() - 1; int b = cin.nextInt() - 1; sub[a] = 1 - sub[a]; sub[b] = 1 - sub[b]; } dp = new int[N, 3]; rec(0, -1); Console.WriteLine(dp[0, 2] + (N - 1)); } static void Main(string[] args) { new Program().myon(); } } class Scanner { string[] s; int i; readonly char[] cs = new char[] { ' ' }; public Scanner() { s = new string[0]; i = 0; } public string next() { if (i < s.Length) return s[i++]; string st = Console.ReadLine(); while (st == "") st = Console.ReadLine(); s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries); if (s.Length == 0) return next(); i = 0; return s[i++]; } public string nextLine() { return Console.ReadLine(); } public int nextInt() { return int.Parse(next()); } public long nextLong() { return long.Parse(next()); } public double nextDouble() { return double.Parse(next()); } }