class UnionFind[T] {

  private[this] var map = Map.empty[T, T];

  def addEdge(parent: T, child: T): Unit = {
    map = map + (child -> parent);
  }

  def fetchRoot(elem: T): T = {
    @scala.annotation.tailrec
    def sub(elem: T, pendings: List[(T, T)]): T = {
      map.get(elem) match {
        case None =>
          pendings.foreach { case (c, p) =>
            map = map + (c -> elem);
          }
          elem;
        case Some(p) =>
          sub(p, (elem, p) :: pendings);
      }
    }
    sub(elem, Nil);
  }

}

object Main extends App {
  val sc = new java.util.Scanner(System.in);
  val n, m = sc.nextInt();
  val ps = Array.fill(n)(sc.nextInt());
  val xys = Array.fill(m)((sc.nextInt(), sc.nextInt()));

  val unionFind = new UnionFind[Int];
  xys.foreach { case (x, y) =>
    val xr = unionFind.fetchRoot(x);
    val yr = unionFind.fetchRoot(y);
    unionFind.addEdge(yr, xr);
  }

  val answer = ps.zipWithIndex.count { case (p, i) =>
    unionFind.fetchRoot(p) == unionFind.fetchRoot(i + 1);
  }
  println(answer);
}