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
  let un = Array.init (n+1) (fun a -> a) in
  Array.iter (fun (x, y) -> un.(x) <- y) xy;
  let ans = ref 0 in
  let rec find elt =
    if un.(elt) = elt then elt else (
      let root = find (un.(elt)) in
      un.(elt) <- root;
      root
    ) in
  for i = 1 to n do (
    if find i = find p.(i-1) then ans := succ !ans;
  ) done;
  print_int !ans;;

let _ = main ()
