module RawUnionFind = struct
  type t = {rank : int array; parent : int array}
  type elt = int
  let make n =
    {rank = Array.make n 0;
     parent = Array.init n (fun c -> c)}

  let rec find uf x =
    if x = uf.parent.(x) then x
    else (
      let y = find uf uf.parent.(x) in
      uf.parent.(x) <- y; y
    )

  let unite uf i j =
    let x = find uf i in
    let y = find uf j in
    if x <> y then (
      if uf.rank.(x) < uf.rank.(y) then uf.parent.(x) <- y
      else (
        uf.parent.(y) <- x;
        if uf.rank.(x) = uf.rank.(y) then
          uf.rank.(x) <- 1 + uf.rank.(x)
      )
    )
end

let () =
  let n, m = Scanf.scanf "%d %d " (fun a b -> a, b) in
  let p = Array.to_list @@ Array.init n (fun _ -> Scanf.scanf "%d " (fun a -> a-1)) in
  let xy = Array.to_list @@ Array.init m (fun _ -> Scanf.scanf "%d %d " (fun a b -> (a-1,b-1))) in

  let uf = RawUnionFind.make (n+1) in
  List.iter (fun (x, y) -> RawUnionFind.unite uf x y) xy;
  List.mapi (fun i p -> RawUnionFind.find uf i = RawUnionFind.find uf p) p
  |> List.filter (fun x -> x)
  |> List.length
  |> Printf.printf "%d\n"
