#[allow(unused_imports)]
use std::cmp::{Ordering};

#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};

#[allow(unused_imports)]
use std::io::{stdin, stdout, BufWriter, Write};

#[allow(unused_imports)]
use std::iter::FromIterator;

#[allow(unused_macros)]
macro_rules! min {
    ($x: expr) => ($x);
    ($x: expr, $($z: expr),+) => {{
        std::cmp::min($x, min!($($z),*))
    }}
}

#[allow(unused_macros)]
macro_rules! max {
    ($x: expr) => ($x);
    ($x: expr, $($z: expr),+) => {{
        std::cmp::max($x, max!($($z),*))
    }}
}

#[allow(unused_macros)]
macro_rules! get { 
    ([$t: ty]) => { 
        { 
            let mut line = String::new(); 
            stdin().read_line(&mut line).unwrap(); 
            line.split_whitespace().map(|t|t.parse::<$t>().unwrap()).collect::<Vec<_>>()
        }
    };
    ([$t: ty];$n: expr) => {
        (0..$n).map(|_|get!([$t])).collect::<Vec<_>>()
    };
    ($t: ty) => {
        {
            let mut line = String::new();
            stdin().read_line(&mut line).unwrap();
            line.trim().parse::<$t>().unwrap()
        }
    };
    ($($t: ty),*) => {
        { 
            let mut line = String::new();
            stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            ($(iter.next().unwrap().parse::<$t>().unwrap(),)*)
        }
    };
    ($t: ty; $n: expr) => {
        (0..$n).map(|_|get!($t)).collect::<Vec<_>>()
    };
    ($($t: ty),*; $n: expr) => {
        (0..$n).map(|_|get!($($t),*)).collect::<Vec<_>>()
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        #[cfg(debug_assertions)]
        writeln!(&mut std::io::stderr(), concat!("[DEBUG] ", $(stringify!($a), "={:?} "),*), $($a),*).unwrap();
    }
}

const BIG_STACK_SIZE: bool = true;

#[allow(dead_code)]
fn main() {
    use std::thread;
    if BIG_STACK_SIZE {
        thread::Builder::new()
            .stack_size(32 * 1024 * 1024)
            .name("solve".into())
            .spawn(solve)
            .unwrap()
            .join()
            .unwrap();
    } else {
        solve();
    }
}

fn dfs(node: usize, nodes: &Vec<Vec<usize>>, depth: &mut Vec<usize>, visited: &mut Vec<bool>, current_depth: usize) {
    visited[node] = true;
    for &next in &nodes[node] {
        if visited[next] {
            continue;
        }
        dfs(next, nodes, depth, visited, current_depth+1);
        visited[next] = true;
    }
    depth[node] = current_depth;
}

fn dfs2(node: usize, nodes: &Vec<Vec<usize>>, visited: &mut Vec<bool>, goal: usize) -> Option<usize> {
    visited[node] = true;
    if node == goal {
        return Some(0);
    }
    for &next in &nodes[node] {
        if visited[next] {
            continue;
        }
        let depth = dfs2(next, nodes, visited, goal);
        visited[next] = true;
        if let Some(depth) = depth {
            return Some(depth+1);
        }
    }
    None
}

fn lower_bound<F>(l: usize, r: usize, criterion: F) -> usize
where
    F: Fn(usize) -> bool,
{
    let mut l = l;
    let mut r = r;
    while r - l > 1 {
        let mid = l + (r - l) / 2;
        if criterion(mid) {
            r = mid;
        } else {
            l = mid;
        }
    }
    l
}

fn solve() {
    let (n,u,v) = get!(usize, usize, usize);
    let ab = get!(usize, usize; n-1);

    if u == v {
        println!("0");
        return;
    }

    let mut nodes = vec![Vec::new(); n+1];
    for (a,b) in ab {
        nodes[a].push(b);
        nodes[b].push(a);
    }

    let mut a_depth = vec![0; n+1];
    let mut t_depth = vec![0; n+1];
    dfs(u, &nodes, &mut t_depth, &mut vec![false; n+1], 0);
    dfs(v, &nodes, &mut a_depth, &mut vec![false; n+1], 0);

    let mut a_depth_inv = BTreeMap::new();
    let mut t_depth_inv = BTreeMap::new();
    for i in 1..n+1 {
        a_depth_inv.entry(&a_depth[i]).or_insert(Vec::new()).push(i);
        t_depth_inv.entry(&t_depth[i]).or_insert(Vec::new()).push(i);
    }

    let mn = dfs2(u, &nodes, &mut vec![false; n+1], v).unwrap();

    let crit = |dist: usize| {
        let vec = a_depth_inv.get(&dist).cloned().unwrap_or(Vec::new());
        for node in vec {
            if t_depth[node] <= dist {
                return false;
            }
        }
        true
    };
    let res = lower_bound(mn, n, crit);
    println!("{}", res-1);
}


Measured memory_bytes: 35196