import std.stdio, std.conv, std.string, std.bigint;
import std.math, std.random, std.datetime;
import std.array, std.range, std.algorithm, std.container;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }

class Node{
	int id;
	int groupId;
	Node[] adjs;
	bool isVisited = 0;
	this(int id){
		this.id = id;
		}
	void setGroupId(int gid){
		this.groupId = gid;
		this.isVisited = true;
		foreach(adj; adjs) if(! adj.isVisited) adj.setGroupId(gid);
		}
	}


void main(){
	auto n = read.to!int;
	auto m = read.to!int;
	
	auto ps = readln.chomp.split.to!(int[]).map!(i => i - 1)().array;
	
	Node[] nodes;
	foreach(i; 0 .. n) nodes ~= new Node(i);
	
	foreach(j; 0 .. m){
		auto x = read.to!int - 1;
		auto y = read.to!int - 1;
		nodes[x].adjs ~= nodes[y];
		nodes[y].adjs ~= nodes[x];
		}
	
	foreach(nd; nodes){
		if(nd.isVisited) continue;
		nd.setGroupId(nd.id);
		}
	
	int count = 0;
	foreach(i; 0 .. n){
		if(nodes[i].groupId == nodes[ps[i]].groupId){
			count += 1;
			}
		}
	
	count.writeln;
	}