module tree_mod
    implicit none
    private
    type node
        integer, pointer:: con(:)
    end type

    public node, init_tree, print_tree, max_edge, dist
contains
    subroutine init_tree(tree, a,b)
        type(node):: tree(:)
        integer(4):: a, b

        call append_tree(tree(a), b)
        call append_tree(tree(b), a)
    end subroutine


    subroutine append_tree(vert, x)
        type(node):: vert
        integer,pointer:: tmp(:)
        integer(4):: ts,x

        ts = size(vert%con(:))
        allocate(tmp(ts))
        tmp(:) = vert%con(:)
        deallocate(vert%con)
        allocate(vert%con(ts+1))
        vert%con(:ts) = tmp(:)
        deallocate(tmp)
        vert%con(ts+1) = x
    end subroutine


    subroutine print_tree(tree)
        type(node):: tree(:)
        integer(4):: i
        character(100):: fmt

        print"('tree--- ---')"
        do i=1, size(tree(:))
            write(fmt,"('(',i0,'(i0,1x))')") size(tree(i)%con(:))+1
            print fmt, i,tree(i)%con(:)
        end do
    end subroutine

    recursive function max_edge(tree, p, is_passed, lev, nu) result(ret)
        type(node):: tree(:)
        integer(4):: p, ret, i, lev,nr,nu
        logical:: is_passed(:)

        if (is_passed(p)) return
        is_passed(p) = .true.
        ! print'("----", i0)', p
        ! print*, is_passed(:)
        ret = 0
        do i=1,size(tree(p)%con(:))
            if (is_passed(tree(p)%con(i))) cycle
            ! print*,'p,lev,dst', p, lev, tree(p)%con(i)
            ! print*, is_passed(:)
            nr = max_edge(tree(:), tree(p)%con(i), is_passed(:), lev+1, nu)
            if (nr > ret) then
                ! print*, 'nr,nu' ,nr,nu
                ret = nr
                nu = tree(p)%con(i)
            end if
        end do
        ret = max(ret,lev)
        ! print*, 'lastnu',nu
    end function

    recursive function dist(tree,a,b, is_passed,nb) result(ret)
        type(node):: tree(:)
        integer(4):: a, b, ret, i, nb, nr
        logical:: is_passed(:)

        ! print*, 'a,b', a,b
        ret = 10000000
        is_passed(a) = .true.
        do i=1, size(tree(a)%con(:))
            if (tree(a)%con(i) == b) then
                ret = 1
                nb = 1
                return
            end if
        end do

        do i=1,size(tree(a)%con(:))
            if (is_passed(tree(a)%con(i))) cycle
            nr = dist(tree(:), tree(a)%con(i),b,is_passed(:),nb) + 1
                if (nr < ret) then
                nb = tree(a)%con(i)
                ! print*, 'ret,nr,nb',ret,nr,nb
                ret = nr
            end if 

        end do

        ! b = nb
    end function
end module tree_mod

program playing_tag_on_tree
    use tree_mod
    implicit none
    integer(4):: n, u, v
    integer(4):: a, b, i, nextv, d,nextu, c
    logical, allocatable:: is_passed(:)
    type(node), allocatable:: tree(:)
    read*, n, u, v
    allocate(tree(n), is_passed(n))
    do i=1, n
        allocate(tree(i)%con(0))
    end do
    do i=1, n-1
        read*, a, b
        call init_tree(tree, a, b)
    end do
    ! call print_tree(tree(:))

    is_passed(:) = .false.
    is_passed(v) = .true.

    ! print*, max_edge(tree(:),1, is_passed(:),0)
nextu=0
c=0
    do while (max_edge(tree(:),u,is_passed(:),0, nextu) /= 0)
        ! print*, 'u,nu',u,nextu
        if(nextu==0) exit
        u = nextu
        is_passed(:) = .false.
        nextv=0
        d = dist(tree(:),v,u,is_passed(:),nextv)
        if (nextv==0) exit
        ! print*, 'v,nv', v, nextv
        v = nextv
        is_passed(:) = .false.
        is_passed(v) = .true.
        c=c+1
        nextu=0
    end do

    print*,c

end program playing_tag_on_tree