import java.util.Scanner;

object Main extends App {
  val sc = new Scanner(System.in);

  val n, m = sc.nextInt();

  val ps = List.fill(n)(sc.nextInt() - 1);

  val xy = List.fill(m)((sc.nextInt() - 1, sc.nextInt() - 1));

  val root = (0 until n).toArray;

  def fetchRoot(i: Int): Int = {
    val r = root(i);
    if (r == i) {
      r;
    } else {
      val r2 = fetchRoot(r);
      root(i) = r2;
      r2;
    }
  }

  xy.foreach { case (x, y) =>
    val rx = fetchRoot(x);
    val ry = fetchRoot(y);
    if (rx != ry) {
      val (rx2, ry2) = if (rx < ry) {
        (rx, ry);
      } else {
        (ry, rx);
      }
      root(ry2) = rx2;
    }
  }

  val answer = (0 until n).filter { i =>
    val p = ps(i);
    val rp = fetchRoot(p);
    val ri = fetchRoot(i);
    ri == rp;
  }.size;

  println(answer);
}
