// Reads multiple ints in a line, separate by a space.
// It works even if the actual number of ints is smaller than n.
// WARNING: This function does not work for -2^31.
// NOTE: This function does not handle error cases.
fun fastReadInts(n: Int): IntArray {
  val xs = IntArray(n)
  val s = readLine()!!
  var currentValue = 0
  var isNegative = false
  var currentIndex = 0
  for (c in s) {
    if (c == ' ') {
      xs[currentIndex] = currentValue * if (isNegative) -1 else 1
      currentValue = 0
      isNegative = false
      currentIndex++
    } else if (c == '-') {
      isNegative = true
    } else {
      currentValue *= 10
      currentValue += c - '0'
    }
  }
  xs[currentIndex] = currentValue * if (isNegative) -1 else 1
  return xs
}

// Implements Union-Find Set.
// Manages integers of [0, n).
class UFSet(n: Int) {
  private var ranks = IntArray(n, { 1 })
  private var prevs = IntArray(n, { it })

  // Returns a root of a tree which x belongs to.
  // The argument x needs to be in [0, n).
  fun find(x: Int): Int {
    if (x != prevs[x]) prevs[x] = find(prevs[x])
    return prevs[x]
  }

  // Merges a set which x belongs to and a set which y belongs to.
  // The argument x and y need to be in [0, n).
  fun union(x: Int, y: Int) {
    val xRoot = find(x)
    val yRoot = find(y)
    when {
      ranks[xRoot] > ranks[yRoot] -> prevs[yRoot] = prevs[xRoot]
      ranks[xRoot] < ranks[yRoot] -> prevs[xRoot] = prevs[yRoot]
      prevs[xRoot] != prevs[yRoot] -> {
        prevs[yRoot] = prevs[xRoot]
        ranks[xRoot]++
      }
    }
  }
}

fun main(args: Array<String>) {
  val (n, m) = fastReadInts(2)
  val ps = fastReadInts(n).map { it - 1 }.toIntArray()

  val ufSet = UFSet(n)

  repeat(m) {
    val (xRaw, yRaw) = fastReadInts(2)
    val x = xRaw - 1
    val y = yRaw - 1
    ufSet.union(x, y)
  }

  val groupToIndexes = Array(n, { mutableListOf<Int>() })
  val groupToValues = Array(n, { mutableListOf<Int>() })

  for (i in 0 until n) {
    val group = ufSet.find(ps[i])
    groupToIndexes[group].add(i)
    groupToValues[group].add(ps[i])
  }

  var ans = 0
  for (i in 0 until n) {
    if (groupToIndexes[i].isEmpty()) continue
    val setIndexes = groupToIndexes[i].toSet()
    val setValues = groupToValues[i].toSet()
    val intersection = setIndexes.intersect(setValues)
    ans += intersection.size
  }
  println(ans)
}
