//#define NDEBUG
#define _CRT_SECURE_NO_WARNINGS
#include <array>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <limits>
#include <memory>
#include <string>
#include <type_traits>

template <class Integral = std::size_t> class irange {
	const Integral f, l;

public:
	using value_type = Integral;
	using reference = value_type;
	class irangeitr {
		friend irange;
		value_type i;
		irangeitr(value_type x) noexcept : i(x) {}

	public:
		void operator++() noexcept { ++i; }
		reference operator*() const noexcept { return i; }
		bool operator!=(const irangeitr &x) const noexcept { return i != x.i; }
	};
	irange(value_type first, value_type last) noexcept : f(first), l(last) {}
	irangeitr begin() const noexcept { return irangeitr(f); }
	irangeitr end() const noexcept { return irangeitr(l); }
};
template <class Integral = std::size_t> class Rirange {
	const Integral f, l;

public:
	using value_type = Integral;
	using reference = value_type;
	class Rirangeitr {
		friend Rirange;
		value_type i;
		Rirangeitr(value_type x) noexcept : i(x) {}

	public:
		void operator++() noexcept { --i; }
		reference operator*() const noexcept { return i - 1; }
		bool operator!=(const Rirangeitr &x) const noexcept { return i != x.i; }
	};
	Rirange(value_type first, value_type last) noexcept : f(first), l(last) {}
	Rirangeitr begin() const noexcept { return Rirangeitr(l); }
	Rirangeitr end() const noexcept { return Rirangeitr(f); }
};
template <class T> bool maxi(T &a, const T &b) {
	if (b < a)
		return 0;
	a = b;
	return 1;
}
template <class T> bool mini(T &a, const T &b) {
	if (a < b)
		return 0;
	a = b;
	return 1;
}
template <class T> bool smaxi(T &a, const T &b) {
	if (a < b) {
		a = b;
		return 1;
	}
	return 0;
}
template <class T> bool smini(T &a, const T &b) {
	if (b < a) {
		a = b;
		return 1;
	}
	return 0;
}
struct customIO {
	int c;
	bool f;
	std::array<int, 30> buf;
	void get_c() { c = fgetc(stdin); }
	void put_c(int x) { fputc(x, stdout); }
	bool vacant() { return c == ' ' || c == '\n'; }
	operator int() { return 0; }

	// in

	void cueing() {
		while (get_c(), vacant())
			;
	}
	template <class T>
	auto operator>>(T &d) ->
		typename std::enable_if<std::is_signed<T>::value, customIO &>::type {
		d = 0;
		f = 0;
		cueing();
		if (c == '-')
			f = 1, get_c();
		do
			d = d * 10 + c - '0';
		while (get_c(), !vacant());
		if (f)
			d = -d;
		return *this;
	}
	template <class T>
	auto operator>>(T &d) ->
		typename std::enable_if<!std::is_signed<T>::value, customIO &>::type {
		d = 0;
		cueing();
		do
			d = d * 10 + c - '0';
		while (get_c(), !vacant());
		return *this;
	}
	customIO &operator>>(char &d) {
		cueing();
		d = c;
		return *this;
	}
	customIO &operator>>(double &d) {
		scanf("%lf", &d);
		return *this;
	}
	customIO &operator>>(std::string &d) {
		d.clear();
		cueing();
		do
			d.push_back(c);
		while (get_c(), !vacant());
		return *this;
	}
	template <class T> T input() {
		T d;
		*this >> d;
		return d;
	}

	// out

	template <class T>
	auto operator<<(T d) ->
		typename std::enable_if<std::is_signed<T>::value, customIO &>::type {
		c = 0;
		f = 0;
		if (d < static_cast<T>(0))
			f = 1, d = -d;
		while (d)
			buf[c++] = d % 10 + '0', d /= 10;
		if (!c)
			buf[c++] = '0';
		if (f)
			put_c('-');
		while (c--)
			put_c(buf[c]);
		return *this;
	}
	template <class T>
	auto operator<<(T d) ->
		typename std::enable_if<!std::is_signed<T>::value, customIO &>::type {
		c = 0;
		while (d)
			buf[c++] = d % 10 + '0', d /= 10;
		if (!c)
			buf[c++] = '0';
		while (c--)
			put_c(buf[c]);
		return *this;
	}
	customIO &operator<<(char d) {
		put_c(d);
		return *this;
	}
	customIO &operator<<(const std::string &d) {
		for (const auto e : d)
			put_c(e);
		return *this;
	}
	customIO &operator<<(const char *d) {
		while (*d != '\0')
			put_c(*(d++));
		return *this;
	}
	customIO &operator<<(double d) {
		/*
		printf("%1.9f",d);
		/*/
		printf("%f", d);
		//*/
		return *this;
	}
	void endl() { put_c('\n'); }
	void space() { put_c(' '); }
} IO;

using int32 = std::int_fast32_t;
using int64 = std::int_fast64_t;
using uint32 = std::uint_fast32_t;
using uint64 = std::uint_fast64_t;
using intl32 = std::int_least32_t;
using intl64 = std::int_least64_t;
using uintl32 = std::uint_least32_t;
using uintl64 = std::uint_least64_t;
#include <cassert>
#include <cstddef>
#include <utility>
#include <vector>

class UnionFind {

public:
	using difference_type = std::ptrdiff_t;
	using container_type = std::vector<difference_type>;
	using size_type = typename container_type::size_type;

private:
	container_type c;

public:
	explicit UnionFind(const size_type size) : c(size, -1) {}

	size_type size() const { return c.size(); }
	bool empty() const { return c.empty(); }

	size_type find(const size_type x) {
		assert(x < size());
		if (c[x] < static_cast<difference_type>(0))
			return x;
		return c[x] = static_cast<difference_type>(find(c[x]));
	}
	bool same(const size_type x, const size_type y) {
		assert(x < size());
		assert(y < size());
		return find(x) == find(y);
	}
	size_type size(const size_type x) {
		assert(x < size());
		return static_cast<size_type>(-c[find(x)]);
	}

	bool unite(size_type x, size_type y) {
		assert(x < size());
		assert(y < size());
		x = find(x);
		y = find(y);
		if (x == y)
			return false;
		if (c[x] > c[y])
			std::swap(x, y);
		c[x] += c[y];
		c[y] = static_cast<difference_type>(x);
		return true;
	}
};
#include <algorithm>
#include <utility>
#include <vector>

int main() {
	uint32 n, m;
	IO >> n >> m;
	std::vector<uint32> a(n);
	for (auto &e : a)
		IO >> e, --e;
	UnionFind T(n);
	while (m--) {
		uint32 x, y;
		IO >> x >> y;
		--x;--y;
		T.unite(x, y);
	}
	uint32 ans = 0;
	for (const auto i : irange<>(0, n))
		if (T.same(i, a[i]))
			++ans;
	IO << ans << '\n';
	return 0;
}

Measured memory_bytes: 1792