package main.scala

import java.util.Scanner

class UnionFind(n: Int) {
  private val pare = Array.ofDim[Int](n)
  private val rk = Array.ofDim[Int](n)

  (0 until n).foreach(i => {
    pare(i) = i
    rk(i) = i
  })

  def find(x: Int): Int = {
    if (x == pare(x)) x
    else {
      pare(x) = find(pare(x))
      pare(x)
    }
  }

  def unite(x: Int, y: Int): Unit = {
    val xx: Int = find(x)
    val yy: Int = find(y)
    if (xx != yy) {
      if (rk(xx) < rk(yy)) pare(xx) = yy
      else pare(yy) = xx
      if (rk(xx) == rk(yy)) rk(xx) += 1
    }
  }

  def same(x: Int, y: Int): Boolean = {
    find(x) == find(y)
  }
}

object Main {
  def main(args: Array[String]): Unit = {
    val sc = new Scanner(System.in)

    val N, M = sc.nextInt
    val p = Array.ofDim[Int](N+1)
    (1 to N).foreach(i => {
      p(i) = sc.nextInt
    })

    val uf = new UnionFind(N+1)
    (1 to M).foreach(_ => {
      val x, y = sc.nextInt
      uf.unite(x, y)
    })

    var ans = 0
    (1 to N).foreach(i => {
      if (uf.same(p(i), i)) ans += 1
    })

    println(ans)
  }
}