Scanf.scanf "%d %d" (fun n m ->
    let p = Array.init n (fun _ -> Scanf.scanf " %d" (fun p -> p - 1)) in
    let rev = Array.make n 0 in
    Array.iteri (fun i v -> rev.(v) <- i) p;
    let par = Array.init n (fun i -> i) in

    let rec root x =
        if x = par.(x) then x else
            let r = root par.(x) in
            let () = par.(x) <- r in
            r
    in

    let merge a b =
        if root a <> root b then par.(par.(b)) <- par.(a)
    in

    for i = 1 to m do
        Scanf.scanf " %d %d" (fun x y ->
            merge (x - 1) (y - 1)
        )
    done;
    let rec loop i acc =
        if i = n then acc else
            let acc = if root i = root rev.(i) then acc + 1 else acc in
            loop (i + 1) acc
    in
    loop 0 0 |> Printf.printf "%d\n"
)