#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<functional>
#include<map>
#include<cstdlib>
#include<complex>
#include<numeric>
#include<set>
#include<deque>
#include<queue>

using namespace std;
using Int = long long;
Int INF = 1LL << 60;
const Int MOD = 1000000000 + 7;

template<Int Modulo = MOD> struct MInt {
	Int val;
	constexpr MInt(Int v = 0) noexcept : val(v% Modulo) {
		if (val < 0) v += Modulo;
	}
	constexpr Int getmod() { return Modulo; }
	constexpr MInt operator - () const noexcept { return val ? Modulo - val : 0; }
	constexpr MInt operator + (const MInt& r) const noexcept { return MInt(*this) += r; }
	constexpr MInt operator - (const MInt& r) const noexcept { return MInt(*this) -= r; }
	constexpr MInt operator * (const MInt& r) const noexcept { return MInt(*this) *= r; }
	constexpr MInt operator / (const MInt& r) const noexcept { return MInt(*this) /= r; }
	constexpr MInt& operator += (const MInt& r) noexcept {
		val += r.val;
		if (val >= Modulo) val -= Modulo;
		return *this;
	}
	constexpr MInt& operator -= (const MInt& r) noexcept {
		val -= r.val;
		if (val < 0) val += Modulo;
		return *this;
	}
	constexpr MInt& operator *= (const MInt& r) noexcept {
		val = val * r.val % Modulo;
		return *this;
	}
	constexpr MInt& operator /= (const MInt& r) noexcept {
		Int a = r.val, b = Modulo, u = 1, v = 0;
		while (b) {
			Int t = a / b;
			a -= t * b; swap(a, b);
			u -= t * v; swap(u, v);
		}
		val = val * u % Modulo;
		if (val < 0) val += Modulo;
		return *this;
	}
	constexpr bool operator == (const MInt& r) const noexcept {
		return this->val == r.val;
	}
	constexpr bool operator != (const MInt& r) const noexcept {
		return this->val != r.val;
	}
	friend ostream& operator << (ostream& os, const MInt<Modulo>& x) noexcept {
		return os << x.val;
	}
	friend istream& operator >> (istream& is, MInt<Modulo>& x) noexcept {
		Int tmp; is >> tmp;
		x = MInt<Modulo>(tmp);
		return is;
	}
	friend constexpr MInt<Modulo> modpow(const MInt<Modulo>& a, Int n) noexcept {
		MInt res(1), tmp = a;
		while (n > 0) {
			if (n & 1) res *= tmp;
			tmp *= tmp;
			n >>= 1;
		}
		return res;
	}
};

using mInt = MInt<>;

struct UnionFind {
	vector<Int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

	UnionFind(Int N) : par(N) { //最初は全てが根であるとして初期化
		for (Int i = 0; i < N; i++) par[i] = i;
	}

	Int root(Int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
		if (par[x] == x) return x;
		return par[x] = root(par[x]);
	}

	void unite(Int x, Int y) { // xとyの木を併合
		Int rx = root(x); //xの根をrx
		Int ry = root(y); //yの根をry
		if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま
		par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
	}

	bool same(Int x, Int y) { // 2つのデータx, yが属する木が同じならtrueを返す
		Int rx = root(x);
		Int ry = root(y);
		return rx == ry;
	}
};

int main() {

	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	Int N, M;
	cin >> N >> M;

	vector<Int> p(N);
	for (Int i = 0; i < N; i++) {
		cin >> p[i];
	}

	vector<Int> x(M);
	vector<Int> y(M);
	for (Int i = 0; i < M; i++) {
		cin >> x[i] >> y[i];
		x[i]--;
		y[i]--;
	}

	UnionFind tree(N);

	for (Int i = 0; i < M; i++) {
		tree.unite(x[i], y[i]);
	}

	Int ans = 0;
	for (Int i = 0; i < N; i++) {
		if (tree.same(i, p[i]-1))ans++;
	}

	cout << ans << "\n";
}

Measured memory_bytes: 3456