fun bd097(args: Array<String>) {
    val numList = args[1].split(" ").map { it.toInt() }
    val numPairList = args.drop(2).map { line -> line.split(" ").map { it.toInt() } }
    val unionFind = UnionFind(numList.size)
    numPairList.forEach {
        unionFind.union(it[0], it[1])
    }
    val ans = numList.filterIndexed { index, num -> unionFind.same(index + 1, num) }.count()
    println(ans)
}

class UnionFind(n: Int) {
    private var parentList: MutableList<Int> =
            (0 until n).map { it }.toMutableList()
    private var rankList: MutableList<Int> =
            (0 until n).map { 1 }.toMutableList()

    fun union(x: Int, y: Int) {
        val xRoot = root(x - 1)
        val yRoot = root(y - 1)
        if (rankList[xRoot] > rankList[yRoot]) {
            parentList[yRoot] = xRoot
        } else {
            parentList[xRoot] = yRoot
        }
    }

    fun same(x: Int, y: Int): Boolean {
        return root(x - 1) == root(y - 1)
    }

    private fun root(x: Int): Int {
        val parent = parentList[x]
        return if (x == parentList[x]) {
            x
        } else {
            val root = root(parent)
            parentList[x] = root
            root
        }
    }
}

fun main(args: Array<String>) {
    bd097(readLineList())
}

fun readLineList(): Array<String> {
    val lineList = mutableListOf<String>()
    while (true) {
        val line = readLine()
        if (line.isNullOrBlank()) {
            break
        } else {
            lineList.add(line!!)
        }
    }
    return lineList.toTypedArray()
}