open Batteries
class union_find n = object (self)
  val mutable par = ( [| |] : int array)
  method init = 
    par <- Array.init n (fun a -> a)
  method get_par = par

  method root s = 
    let rec loop x =
      if par.(x) = x then x else (par.(x) <- loop par.(x); par.(x))
    in loop s

  method rank s =
    let rec loop x n =
      if par.(x) = x then n else loop par.(x) (n+1)
    in loop s 1

  method unite x y =
    let xrank = self#rank x in
    let yrank = self#rank x in
    let rx = self#root x in
    let ry = self#root y in
    if rx != ry then (if xrank < yrank then par.(rx) <- ry else par.(ry) <- rx)

  method same x y = self#root x = self#root y
end

let nm = Scanf.sscanf (read_line()) "%d %d" (fun n m ->
  [n; m]
)
let n = List.hd nm
let m = List.nth nm 1
let p = read_line () |> String.split_on_char ' ' |> List.map int_of_string
let p_l = List.length p
let tree = new union_find p_l
let _ = tree#init
let count t l = List.fold_left (fun acc k -> if k = t then acc + 1 else acc) l
let rec loop i =
  match i with
  | i when i = m -> ()
  | _ -> Scanf.sscanf (read_line()) "%d %d" (fun x y -> tree#unite (x-1) (y-1)); loop (i+1)
let _ = loop 0
let _ = List.map (fun k -> tree#root k) @@ Array.to_list tree#get_par

let rec loop2 lst s i =
  match lst with
  | [] -> i
  | first :: rest -> if tree#same first ((List.nth p s)-1) then loop2 rest (s+1) (i+1) else loop2 rest (s+1) i

let _ = Printf.printf "%d\n" @@ loop2 (Array.to_list tree#get_par) 0 0
