#![allow(unused_imports)]
#![cfg_attr(feature = "cargo-clippy", allow(redundant_field_names))]

use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
use std::io::{self, BufWriter, Read, Write};
use std::iter::FromIterator as _FromIterator;
use std::ops::{Add, Div, Mul, Sub};
use std::str::{self, FromStr};
use std::{cmp, fmt, mem};

fn main() {
    let (ps, xys): (Vec<usize>, Vec<(usize, usize)>) = {
        let mut sc = InputScanOnce::new(io::stdin(), 1024);
        let (n, m) = (sc.next(), sc.next());
        (sc.vec(n), sc.pairs(m))
    };
    let ps = ps.into_iter().map(|p| p - 1).collect::<Vec<_>>();
    let mut t = UnionTree {
        ranks: vec![0; ps.len()],
        edges: (0..ps.len()).collect(),
    };
    for (ix, iy) in xys.into_iter().map(|(ix, iy)| (ix - 1, iy - 1)) {
        t.insert(ps[ix], ps[iy]);
    }
    let r = ps.into_iter()
        .enumerate()
        .filter(|&(i, p)| t.search(i) == t.search(p))
        .count();
    println!("{}", r);
}

struct UnionTree {
    edges: Vec<usize>,
    ranks: Vec<u64>,
}

impl UnionTree {
    fn insert(&mut self, x: usize, y: usize) {
        let rx = self.search(x);
        let ry = self.search(y);
        if rx != ry {
            if self.ranks[rx] < self.ranks[ry] {
                self.edges[rx] = ry;
                self.ranks[rx] = self.ranks[ry];
            } else {
                self.edges[ry] = rx;
                self.ranks[ry] = self.ranks[rx];
            }
        }
    }

    fn search(&mut self, x: usize) -> usize {
        let c = self.edges[x];
        if c == x {
            x
        } else {
            let r = self.search(c);
            self.edges[x] = r;
            r
        }
    }
}

struct InputScanOnce {
    buf: Vec<u8>,
    pos: usize,
}

#[allow(dead_code)]
impl InputScanOnce {
    fn new<R: Read>(mut reader: R, estimated: usize) -> Self {
        let mut buf = Vec::with_capacity(estimated);
        let _ = io::copy(&mut reader, &mut buf).unwrap();
        InputScanOnce { buf: buf, pos: 0 }
    }

    #[inline]
    fn next<T: FromStr>(&mut self) -> T
    where
        T::Err: fmt::Debug,
    {
        let mut start = None;
        loop {
            match (self.buf[self.pos], start.is_some()) {
                (b' ', true) | (b'\n', true) => break,
                (_, true) | (b' ', false) | (b'\n', false) => self.pos += 1,
                (_, false) => start = Some(self.pos),
            }
        }
        let target = &self.buf[start.unwrap()..self.pos];
        unsafe { str::from_utf8_unchecked(target) }.parse().unwrap()
    }

    fn vec<T: FromStr>(&mut self, n: usize) -> Vec<T>
    where
        T::Err: fmt::Debug,
    {
        (0..n).map(|_| self.next()).collect()
    }

    fn pairs<T1: FromStr, T2: FromStr>(&mut self, n: usize) -> Vec<(T1, T2)>
    where
        T1::Err: fmt::Debug,
        T2::Err: fmt::Debug,
    {
        (0..n).map(|_| (self.next(), self.next())).collect()
    }

    fn trios<T1: FromStr, T2: FromStr, T3: FromStr>(&mut self, n: usize) -> Vec<(T1, T2, T3)>
    where
        T1::Err: fmt::Debug,
        T2::Err: fmt::Debug,
        T3::Err: fmt::Debug,
    {
        (0..n)
            .map(|_| (self.next(), self.next(), self.next()))
            .collect()
    }

    fn mat<T: FromStr>(&mut self, m: usize, n: usize) -> Vec<Vec<T>>
    where
        T::Err: fmt::Debug,
    {
        (0..m).map(|_| self.vec(n)).collect()
    }

    fn strings_as_mat<T, F: FnMut(u8) -> T>(&mut self, h: usize, mut f: F) -> Vec<Vec<T>> {
        (0..h)
            .map(|_| {
                let l = self.next::<String>();
                l.as_bytes().iter().cloned().map(&mut f).collect()
            })
            .collect()
    }
}


Measured memory_bytes: 10364