import std.stdio, std.string, std.conv;
import std.typecons;
import std.algorithm, std.array, std.range, std.container;
import std.math;
 
void main() {
	auto data = readln.split.to!(long[]), N = data[0], M = data[1];
	auto p = readln.split.to!(long[]).map!(x=>x-1);
	
	long[] list = N.iota().array;
	//long[] size = repeat(1L, N).array;
	ulong root(ulong num) {
		auto parent = list[num];
		return parent == num ? num : ( list[num] = root(list[num]) ) ;
	}
	
	void unite(ulong num1, ulong num2) {
		auto root1 = root(num1), root2 = root(num2);
		if (root1 != root2) {
			//auto result = size[root1] * size[root2];
			list[root1] = root2;
			//size[root2] += size[root1];
			
		}
	}
	
	bool same(ulong num1, ulong num2) { return root(num1) == root(num2); }
	
	foreach (i; 0 .. M) {
		data = readln.split.to!(long[]),
		unite(data[0]-1, data[1]-1);
	}
	
	long ans = 0;
	N.iota.each!(i => same(i, p[i])? ans++ : 0);
	writeln(ans);
}