;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;文字列 -> 整数
(defn Int [s] (Integer/parseInt s))
;文字列 -> 文字列
(defn Str [s] s)

;scanf的なノリで1行をいい感じに読み取る
;定数が重いので注意
(defn input [varTypes]
    (mapv 
        (fn [f s] (f s)) ;文字列sに関数fを適用する
        varTypes         ;適用する関数
        (clojure.string/split 
            (read-line)  ;1行を読み込み
            #" "         ;区切り文字
        )
    )
)

;スペース区切りの整数を受け取る
;速い
(defn inputFast [varType]
    (map
        varType
        (clojure.string/split 
            (read-line)  ;1行を読み込み
            #" "         ;区切り文字
        )
    )
)

(defn floor [a b] (quot a b))
(defn ceil [a b] (floor (- (+ a b) 1) b))

(defprotocol UF
    (find-par [this ^ints v])
    (unite  [this ^ints u ^ints v])
    (same [this ^ints u ^ints v])
)

(deftype UnionFind [^ints par ^ints rank]
    UF
    (find-par [this v]
        (let [p (aget ^ints par v)]
            (if (= p v)
                v
                (let [pp (find-par this p)]
                    (aset ^ints par v pp)
                    pp
                )
            )
        )
    )
    (unite [this u v]
        (let [x (find-par this u) y (find-par this v)]
            (if (= x y)
                false          
                (let [x-rank (aget ^ints rank x) y-rank (aget ^ints rank y)]
                    (if (< x-rank y-rank)
                        (aset ^ints par x y)
                        (do
                            (if (= x-rank y-rank)
                                (aset ^ints rank x (+ 1 x-rank))                            
                            )
                            (aset ^ints par y x)
                        )
                    )
                    true
                )
            )     
        )
    )
    (same [this u v]
        (= (find-par this u) (find-par this v))        
    )
)

(defn UF-build [^ints n]
    (dosync 
        (let [par (int-array n) rank (int-array n)]
            (doseq [i (range n)]
                (aset ^ints par i i)
                (aset ^ints rank i 0)
            )
            (UnionFind. par rank)
        )
    )
)

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
(dosync (do
    (let [[N M] (inputFast Int)]
        (def p (inputFast Int))
        (def uf (UF-build N))
        (doseq [i (range M)]  
            (let [[x y] (inputFast Int)]
                ;(.unite uf (- x 1) (- y 1))
                1
            )
        )
        ;(let [ret (ref 0)]
        ;    (doseq [i (range N)]
        ;        (if (.same uf (- (nth p i) 1) i)
        ;            (alter ret + 1)
        ;        )
        ;    )
        ;    (println (deref ret))
        ;)
    )
    ;(def uf (UF-build 5))
    ;(println (.unite uf 2 3))
    ;(println (.unite uf 3 4))
    ;(println (.unite uf 2 4))
    ;(doseq [i (range 0 5)]
    ;    (println (.find-par uf i))
    ;)
))