#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#include<bits/stdc++.h>
#define MAXN 200005
using namespace std;

int n,cnt,last[MAXN];
int father[MAXN],deep[MAXN],dis[MAXN];
int q[MAXN],nnn[MAXN],mx[MAXN];
int tim,a[MAXN][2],xx,yy;
bool vis[MAXN],win[MAXN];

struct edge
{
    int to,next,h;
}t[MAXN*4];

void add(int u,int v,int h)
{
    t[++cnt].to=v;t[cnt].h=h;
    t[cnt].next=last[u];last[u]=cnt;
    t[++cnt].to=u;t[cnt].h=h;
    t[cnt].next=last[v];last[v]=cnt;
}

void dfs(int x)
{
    deep[x]=deep[father[x]]+1;nnn[x]=++tim;
    for (int i=last[x];i;i=t[i].next)
        if (t[i].to!=father[x]) father[t[i].to]=x,dfs(t[i].to);
    mx[x]=++tim;
}

bool check(int x,int y)
{
    if (nnn[x]>nnn[y]) swap(x,y);
    if (nnn[x]<nnn[y]&&mx[x]>nnn[y]) return deep[y]-deep[x]>2 ? true : false;
    if (father[x]==father[y]) return false;
    return true;
}

void bfs()
{
    int h=1,tt=1;q[1]=xx;vis[xx]=1;
    while (h<=tt)
    {
        int x=q[h++];
        for (int i=last[x];i;i=t[i].next)
            if (!t[i].h&&!vis[t[i].to])
            {
                dis[t[i].to]=dis[x]+1;
                if (dis[t[i].to]<deep[t[i].to]) vis[t[i].to]=1,q[++tt]=t[i].to;
            }
    }
}

int max(int a,int b){return a>b?a:b;}

template<class T>inline void read(T &res)
{
    static char ch;T flag=1;
    while((ch=getchar())<'0'||ch>'9')if(ch=='-')flag=-1;res=ch-48;
    while((ch=getchar())>='0'&&ch<='9')res=res*10+ch-48;res*=flag;
}

int main()
{
    int ans=0;
    read(n);read(xx);read(yy);

    for (int i=1;i<n;i++) {read(a[i][0]);read(a[i][1]);}
    for (int i=1;i<n;i++)
    {
        int x,y;
        read(x);read(y);
        add(x,y,1);
    }

    deep[0]=-1;
    dfs( yy );

    for (int i=1;i<n;i++)
    {
        if (check(a[i][0],a[i][1]))
            win[a[i][0]]=win[a[i][1]]=1;
        else add(a[i][0],a[i][1],0);
    }

    bfs();

    for (int i=1;i<=n;i++)
        if (win[i]&&vis[i])
            {putchar('-');putchar('1');return 0;}

    for (int i=1;i<=n;i++)
        if (vis[i])
            ans=max(ans,deep[i]<<1);

    printf("%d\n",ans);
    return 0;
}

Measured memory_bytes: 21120