Problem:
Score : 1400 points

Problem Statement

Sigma and Sugim are playing a game.

The game is played on a graph with N vertices numbered 1 through N. The graph has N-1 red edges and N-1 blue edges, and the N-1 edges in each color forms a tree. The red edges are represented by pairs of integers (a_i, b_i), and the blue edges are represented by pairs of integers (c_i, d_i).

Each player has his own piece. Initially, Sigma's piece is at vertex X, and Sugim's piece is at vertex Y.

The game is played in turns, where turns are numbered starting from turn 1. Sigma takes turns 1, 3, 5, ..., and Sugim takes turns 2, 4, 6, ....

In each turn, the current player either moves his piece, or does nothing. Here, Sigma can only move his piece to a vertex that is directly connected to the current vertex by a red edge. Similarly, Sugim can only move his piece to a vertex that is directly connected to the current vertex by a blue edge.

When the two pieces come to the same vertex, the game ends immediately. If the game ends just after the operation in turn i, let i be the total number of turns in the game.

Sigma's objective is to make the total number of turns as large as possible, while Sugim's objective is to make it as small as possible.

Determine whether the game will end in a finite number of turns, assuming both players plays optimally to achieve their respective objectives. If the answer is positive, find the number of turns in the game.

Constraints

2 ≦ N ≦ 200,000

1 ≦ X, Y ≦ N

X \neq Y

1 ≦ a_i, b_i, c_i, d_i ≦ N

The N-1 edges in each color (red and blue) forms a tree.

Input

The input is given from Standard Input in the following format:

N X Y
a_1 b_1
a_2 b_2
:
a_{N-1} b_{N-1}
c_1 d_1
c_2 d_2
:
c_{N-1} d_{N-1}

Output

If the game will end in a finite number of turns, print the number of turns.
Otherwise, print -1.

Sample Input 1

4 1 2
1 2
1 3
1 4
2 1
2 3
1 4

Sample Output 1

4

Sample Input 2

3 3 1
1 2
2 3
1 2
2 3

Sample Output 2

4

Sample Input 3

4 1 2
1 2
3 4
2 4
1 2
3 4
1 3

Sample Output 3

2

Sample Input 4

4 2 1
1 2
3 4
2 4
1 2
3 4
1 3

Sample Output 4

-1

Sample Input 5

5 1 2
1 2
1 3
1 4
4 5
2 1
1 3
1 5
5 4

Sample Output 5

6

Solution in C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Text;
using System.Collections.Generic;
class Solve{
    List<int>[] G;
    int[] depth;
    int[] parent;
    bool[] b;
    int ans;
    public Solve(){}
    StringBuilder sb;
    public static int Main(){
        new Solve().Run();
        return 0;
    }
    void Run(){
        sb = new StringBuilder();
        Calc();
        Console.Write(sb.ToString());
    }
    void Calc(){
        int N,X;
        {
            string[] str = Console.ReadLine().Split(' ');
            N = int.Parse(str[0]);
            X = int.Parse(str[1])-1;
            int Y = int.Parse(str[2])-1;
            G = new List<int>[N];
            for(int i=0;i<N;i++){
                G[i] = new List<int>();
            }
            for(int i=0;i<N-1;i++){
                str = Console.ReadLine().Split(' ');
                int a = int.Parse(str[0])-1;
                int b = int.Parse(str[1])-1;
                G[a].Add(b);
                G[b].Add(a);
            }
            List<int>[] T = new List<int>[N];
            for(int i=0;i<N;i++){
                T[i] = new List<int>();
            }
            for(int i=0;i<N-1;i++){
                str = Console.ReadLine().Split(' ');
                int a = int.Parse(str[0])-1;
                int b = int.Parse(str[1])-1;
                T[a].Add(b);
                T[b].Add(a);
            }
            Tree Tr = new Tree(T);
            depth = Tr.GetDepth(Y);
            parent = Tr.GetParent(Y);
        }
        b = new bool[N];
        ans = 0;
        dfs(X,0);
        if(ans != -1){
            ans *= 2;
        }
        sb.Append(ans+"\n");
    }
    void dfs(int v,int d){
        b[v] = true;
        if(depth[v] < d){
            return;
        }
        if(ans != -1 && ans < depth[v]){
            ans = depth[v];
        }
        if(depth[v] > d){
            if(check(v)){
                ans = -1;
                return;
            }
            for(int i=0;i<G[v].Count;i++){
                int t = G[v][i];
                if(!b[t]){
                    dfs(t,d+1);
                }
            }
        }
    }
    bool check(int v){
        for(int i=0;i<G[v].Count;i++){
            int t = G[v][i];
            if(parent[v] == parent[t] || (parent[v] != -1 && parent[parent[v]] == t) || (parent[t] != -1 && parent[parent[t]] == v) || parent[t] == v || parent[v] == t){
                ;         
            }
            else{
                return true;
            }
        }
        return false;
    }
}
public class Tree{
    List<int>[] T;
    bool[] b;
    int N;
    int temp1;
    int temp2;
    int[] depth;
    int[] parent;
    public int center1;
    public int center2;
    public int diamater;
    public Tree(List<int>[] G){
        T = G;
        N = G.Length;
    } 
    void calcLongestPath(int v){
        b = new bool[N];
        temp1 = 0;
        temp2 = 0;
        dfs1(v,0);
    }
    public int LongestPathDistance(int v){
        calcLongestPath(v);
        return temp1;
    }
    public int LongestPath(int v){
        calcLongestPath(v);
        return temp2;
    }
    public void Diamater(){
        int x1 = LongestPath(0);
        int x2 = LongestPath(x1);
        diamater = temp1;
    }
    public void Center(){
        int x1 = LongestPath(0);
        int x2 = LongestPath(x1);
        diamater = temp1;
        if(diamater % 2 == 0){
            center1 = Search(x1,x2,diamater/2);
            center2 = -1;
        }
        else{
            center1 = Search(x1,x2,diamater/2);
            center2 = Search(x1,x2,diamater/2+1);
        }
    }
    public int Search(int s,int t,int l){
        b = new bool[N];
        dfs2(s,t,0,l);
        return temp1;
    }
    public int[] GetDepth(int p){
        b = new bool[N];
        depth = new int[N];
        dfs3(p,0);
        return depth;
    }
    public int[] GetParent(int p){
        b = new bool[N];
        parent = new int[N];
        dfs4(p,-1);
        return parent;
    }
    void dfs1(int v,int l){
        b[v] = true;
        for(int i=0;i<T[v].Count;i++){
            int t = T[v][i];
            if(!b[t]){
                dfs1(t,l+1);
            }
        }
        if(l >= temp1){
            temp1 = l;
            temp2 = v;
        }
    }
    bool dfs2(int v,int o,int l,int d){
        b[v] = true;
        bool x = v == o;
        for(int i=0;i<T[v].Count;i++){
            int t = T[v][i];
            if(!b[t]){
                x |= dfs2(t,o,l+1,d);
            }
        }
        if(x && l == d){
            temp1 = v;
        }
        return x;
    }
    void dfs3(int v,int d){
        b[v] = true;
        depth[v] = d;
        for(int i=0;i<T[v].Count;i++){
            int t = T[v][i];
            if(!b[t]){
                dfs3(t,d+1);
            }
        }
    }
    void dfs4(int v,int p){
        b[v] = true;
        parent[v] = p;
        for(int i=0;i<T[v].Count;i++){
            int t = T[v][i];
            if(!b[t]){
                dfs4(t,v);
            }
        }
    }
}