#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define tr(container, it) \
    for (auto it = container.begin(); it != container.end(); it++)
#define scontains(c, x) ((c).find(x) != (c).end())   //O(log n)
#define contains(c, x) (find((c).begin(),(c).end(),x) != (c).end()) //O(n)
#define ill(_x)  ll _x;scanf("%lld",&_x);
#define idb(_x)  double _x;scanf("%lf",&_x);
#define pll pair<ll,ll>
#define mll map<ll,ll>
#define vll vector<ll>
#define sll set<ll>
#define vs vector<string>
#define in0(x, a, b)((x)>=a && (x)<=b    )
#define in1(x, a, b)((x)>a && (x)<b)
#define  rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
const double pi = 3.14159265358979323846;
//const int INF = 0x3f3f3f3f;


int n, m;
class Graph
{
	int V;    // No. of vertices

			  // Pointer to an array containing adjacency
			  // lists
//	vector<int> *adj;
public:
	Graph(int V);  // Constructor

				   // function to add an edge to graph
	void addEdge(int v, int w);
	void addEdgeDirected(int v, int w);

	// prints BFS traversal from a given source s
	void BFS(int s);
	void DFS(int s);
	void DFSs(int s, bool[]);
};

vector<int > adj[100002];
Graph::Graph(int V)
{
	this->V = V;
//	adj = new vector<int>[V];
}

void Graph::addEdge(int v, int w)
{
	adj[v].push_back(w);
	adj[w].push_back(v); // Undirected
}

void Graph::addEdgeDirected(int v, int w)
{
	adj[v].push_back(w);
	//	adj[w].push_back(v); // Undirected
}

void Graph::BFS(int s)
{
	// Mark all the vertices as not visited
	bool *visited = new bool[V];
	for (int i = 0; i < V; i++)
		visited[i] = false;

	// Create a queue for BFS
	queue<int> queue;
	visited[s] = true;
	queue.push(s);

	while (!queue.empty())
	{
		// Dequeue a vertex from queue and print it
		s = queue.front();
		cout << s << "\n";
		queue.pop();
//		std::cout << "visiting" << s << "\n";
		for (auto i = adj[s].begin(); i != adj[s].end(); ++i)
		{
			if (!visited[*i])
			{
				visited[*i] = true;
				queue.push(*i);
			}
		}
	}
}

vector<vector<int> > vvs;
bool visited[100002];
bool u[100002];
vector<int> v;
void Graph::DFS(int s)
{
    v.clear();
    v.emplace_back(s);
	DFSs(s, visited);
	if(v.size()>1)vvs.emplace_back(v);
}

void Graph::DFSs(int s, bool visited[])
{
	visited[s] = true;
//	cout << s << " ";
	for (auto i = adj[s].begin(); i != adj[s].end(); ++i)
	{
		if (!visited[*i])
		{
            v.emplace_back(*i);
			visited[*i] = true;
            u[s]= false;
            u[*i]= false;
			DFSs(*i, visited);
		}
	}
}



//set<string>A;
//void _() {
//    string s;
//    cin >> s;
//    ll a;
//    cin >> a;
//    ll sz=s.size();
//    ll t=0;
//    for (int i = 0; i < sz; ++i) {
//        for (int j = i; j < sz; ++j) {
//            A.insert(s.substr(i,j-i+1));
//        }
//    }
//    auto it = A.begin();
//
//
//    std::advance(it, a-1);
//    cout << *it;
//
//
//
//
//
//};

int A[100009];
Graph graph(n);
set<int>s1;
set<int>s2;
set<int>intersect;
void _(){
    scanf("%d %d",&n,&m );
    for (int i = 0; i < n; ++i) {
        scanf("%d",A+i);
    }
    memset(visited,0,sizeof(bool)*n);
    memset(u,1,sizeof(bool)*n);
    int a,b;

    for (int i = 0; i < m; ++i) {
        scanf("%d %d",&a,&b );
        graph.addEdge(a-1,b-1);
    }
    for (int i = 0; i < n; ++i) {
        if(!visited[i])graph.DFS(i);
    }
    ll sss=0;
    for (int i = 0; i < n; ++i) {
        if(u[i] && i+1==A[i])sss++;
    }
    for(auto vv : vvs){
        s1.clear();
        s2.clear();
        intersect.clear();
        for(auto af : vv){
            s1.insert(af);
        }

        for(auto aa :s1){
            s2.insert(A[aa]-1);
        }
        set_intersection(s1.begin(),s1.end(),s2.begin(),s2.end(),
                         std::inserter(intersect,intersect.begin()));

        if(!intersect.empty())
        sss+=distance(intersect.begin(),intersect.end());
    }
    cout << sss;



}


int main() {
#if __MINGW32__
    freopen("E:\\Workspace\\Desktop\\cpp\\IO\\Input.txt", "r", stdin);
    freopen("E:\\Workspace\\Desktop\\cpp\\IO\\Output.txt", "w", stdout);
#endif

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    _();
    return 0;

}








Measured memory_bytes: 24568