let un = Array.init 100010 (fun a -> a)

let depth = Array.make 100010 0

let rec find elt =
if un.(elt) = elt then elt else (
    let root = find (un.(elt)) in
    un.(elt) <- root;
    root
  )

let rec unite x y =
  let x = find x in
  let y = find y in
  if x == y then ()
  else if depth.(x) < depth.(y) then un.(x) <- y
  else (
    un.(y) <- x;
    if depth.(x) = depth.(y) then depth.(x) <- depth.(x) + 1
  )

let main () =
  let n, m = Scanf.scanf "%d %d\n" (fun a b -> a, b) in
  let p = Array.init n (fun _ -> Scanf.scanf "%d " (fun a -> a)) in
  let xy = Array.init m (fun _ -> Scanf.scanf "%d %d\n" (fun a b -> a, b)) in
  Array.iter (fun (x, y) -> unite x y) xy;
  let ans = ref 0 in
  for i = 1 to n do (
    if find i = find p.(i-1) then ans := succ !ans;
  ) done;
  print_int !ans;;

let _ = main ()
