#include <stdio.h>
#include <assert.h>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <array>
#include <vector>
#include <queue>
#include <set>
#include <cmath>
//#include <unordered_map>
//#include <unordered_set>
//#include <boost/container/static_vector.hpp>
//#include <boost/unordered_set.hpp>
//#include <boost/unordered_map.hpp>
//#include <unistd.h>

//#include <cv.h>
//#include <highgui.h>
#include <stdlib.h>
#include <time.h>

#include <string>

const int MAX_N = 100000;
const int MAX_M = 100000;

int p_in[MAX_N+1];
int x_in[MAX_M+1], y_in[MAX_M+1];
int N, M;

int par[MAX_N+1];
std::vector<int> ch[MAX_N+1];

bool tmp[MAX_N+1];

int root(int x) {
    if (par[x] == x) return x;
    return par[x] = root(par[x]);
}

void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry) return;
    par[rx] = ry;
}

bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
}

int count_identical(const std::vector<int>& vec1, const std::vector<int>& vec2) {
    assert(vec1.size() == vec2.size());
    int ret = 0;
    for (int i = 0; i < vec1.size(); i++) {
        //std::cout << vec1[i] << " " << vec2[i] << std::endl;
        tmp[vec1[i]] = true;
    }
    for (int i = 0; i < vec2.size(); i++) {
        if (tmp[vec2[i]]) { ret++; }
    }
    for (int i = 0; i < vec1.size(); i++) {
        tmp[vec1[i]] = false;
    }
    //std::cout << ret << std::endl;
    return ret;
}

int solve() {
    for (int i = 0; i <= N; i++) { par[i] = i; ch[i].clear(); }
    for (int i = 1; i <= M; i++) {
        unite(x_in[i], y_in[i]);
    }

    for (int i = 0; i <= N; i++) {
        ch[root(i)].push_back(i);
    }

    for (int i = 0; i <= N; i++) {
        //std::sort(ch[i].begin(), ch[i].end());
        tmp[i] = false;
        //std::cout << "check:" << i << " " << root(i) << std::endl;
    }

    int ret = 0;
    for (int i = 1; i <= N; i++) {
        //std::cout << "root:" << i << std::endl;
        std::vector<int> nums;
        for (int j = 0; j < ch[i].size(); j++) {
            nums.push_back(p_in[ch[i][j]]);
        }
        ret += count_identical(ch[i], nums);
    }
    return ret;
}


int main(int argc, char **argv) {
    std::cin >> N >> M;
    for (int i = 1; i <= N; i++) {
        std::cin >> p_in[i];
    }
    for (int i = 1; i <= M; i++) {
        std::cin >> x_in[i] >> y_in[i];
    }
    /*
    N = 5;
    M = 2;
    p_in[1] = 5; p_in[2] = 3; p_in[3] = 1; p_in[4] = 4; p_in[5] = 2;
    x_in[1] = 1; y_in[1] = 3;
    x_in[2] = 5; y_in[2] = 4;
    */
    /*
    N = 3;
    M = 2;
    p_in[1] = 3; p_in[2] = 2; p_in[3] = 1;
    x_in[1] = 1; y_in[1] = 2;
    x_in[2] = 2; y_in[2] = 3;
    */
    /*
    N = 10;
    M = 8;
    p_in[1] = 5; p_in[2] = 3; p_in[3] = 6;
    p_in[4] = 8; p_in[5] = 7; p_in[6] = 10;
    p_in[7] = 9; p_in[8] = 1; p_in[9] = 2;
    p_in[10] = 4;
    x_in[1] = 3; y_in[1] = 1;
    x_in[2] = 4; y_in[2] = 1;
    x_in[3] = 5; y_in[3] = 9;
    x_in[4] = 2; y_in[4] = 5;
    x_in[5] = 6; y_in[5] = 5;
    x_in[6] = 3; y_in[6] = 5;
    x_in[7] = 8; y_in[7] = 9;
    x_in[8] = 7; y_in[8] = 9;
    */
    std::cout << solve() << std::endl;
    return 0;
}


Measured memory_bytes: 7424