import scala.io.{StdIn => in}

object Main extends App {
  val Array(n, m) = in.readLine.split(" ").map(_.toInt)
  val seqP = in.readLine.split(" ").map(_.toInt-1)
  val uft = new UnionFindTree(n)
  Iterator.continually(in.readLine)
    .take(m)
    .map(_.split(" ").map(_.toInt - 1))
    .foreach { e =>
      uft.unite(e(0), e(1))
    }
    
    val ans = (0 until n)./:(0)((acc,i) => {
      if(uft.same(i, seqP(i))) acc+1 else acc
    })

  println(ans)

  class UnionFindTree(n:Int) {
    private val par  = Array.range(0, n)
    private val rank = Array.fill(n)(1)

    def root(x:Int):Int = {
      if(par(x) == x) x
      else { par(x) = root(par(x)); par(x) }
    }

    def same(x:Int, y:Int):Boolean = root(x) == root(y)

    def unite(x:Int, y:Int):Unit = {
      val rx = root(x)
      val ry = root(y)
      if(rx == ry) return

      if(rank(rx) < rank(ry)) par(rx) = ry
      else {
        par(ry) = rx
        if(rank(rx) == rank(ry)) rank(rx) += 1
      }
    }
  }
}