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 = N;
	bool[] mark; mark.length = p.length;
	long m;
	while (m < N) {
		/*long[] cycle; long num = 10^^5+4;
		foreach (i, flag; mark) if (!flag) { num = cast(long)(i); break; }
		if (num == 10^^5+4) break;
		
		mark[num] = true; cycle = [num];
		
		while (true) {
			auto next = p[num];
			//writeln(num, next);
			if (mark[next]) break;
			else cycle ~= next, mark[next] = true, num = next;
		}*/
		if (mark[m]) { ++m; continue; }
		long[] cycle = [m];
		mark[m] = true;
		while (true) {
			auto next = p[m];
			if (mark[next]) break;
			cycle ~= next, mark[next] = true, m = next;
		}
		//writeln(cycle);
		
		if (cycle.length == 1) { continue; }
		
		
		deal:
		ulong n = 0;
		while (n < cycle.length) {
			/+if (cycle.length == 0 || cycle.length == 1) { cycle = []; break; }
			if (same(cycle[$-1], cycle[0])) { /*writeln("here");*/ cycle = cycle[1 .. $]; /*writeln(cycle);*/ continue; }
			foreach (i; 0 .. cycle.length-1)
				if (same(cycle[i], cycle[i+1])) {
					//writeln("here", i+1);
					cycle = cycle[0 .. i+1] ~ cycle[i+2 .. $];
					//writeln(cycle);
					continue label;
				}
			
			break;+/
			if (n == 0) {
				//writeln(cycle, n, " ", cycle[$-1]);
				if (same(cycle[$-1], cycle[0])) { cycle = cycle[1 .. $]; /*writeln("here0");*/ }
				else n++;
			}
			else {
				//writeln(cycle, n);
				if (same(cycle[n-1], cycle[n])){ cycle = cycle[0 .. n] ~ cycle[n+1 .. $]; /*writeln("here1");*/ }
				else n++;
			}
			
		}
		if (cycle.length >= 2 && same(cycle[$-1], cycle[0])) goto deal;
		
		if (cycle.length == 1) cycle = [];
		ans -= cycle.length;
		//writeln(ans);
		
	}
	writeln(ans);
}
