#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <list>
#include <math.h>
#include <random>
#include <deque>
#include <queue>
using namespace std;
typedef long long ll;
#define fori(n) for ( int i = 0; i < (n);i++)
#define forj(n) for ( int j = 0; j < (n);j++)
#define fork(n) for ( int k = 0; k < (n);k++)
#define forghj(n) for ( int ghj = 0; ghj < (n);ghj++)
#define fori1(n) for ( int i = (n)-1; i >= 0;i--)
#define all(vector) (vector).begin(),(vector).end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
/*
                            |~~~~~~~|
                            |       |
                            |       |
                            |       |
                            |       |
                            |       |
 |~.\\\_\~~~~~~~~~~~~~~xx~~~         ~~~~~~~~~~~~~~~~~~~~~/_//;~|
 |  \  o \_         ,XXXXX),                         _..-~ o /  |
 |    ~~\  ~-.     XXXXX`)))),                 _.--~~   .-~~~   |
  ~~~~~~~`\   ~\~~~XXX' _/ ';))     |~~~~~~..-~     _.-~ ~~~~~~~
           `\   ~~--`_\~\, ;;;\)__.---.~~~      _.-~
             ~-.       `:;;/;; \          _..-~~
                ~-._      `''        /-~-~
                    `\              /  /
                      |         ,   | |
                       |  '        /  |
                        \/;          |
                         ;;          |
                         `;   .       |
                         |~~~-----.....|
                        | \             \
                       | /\~~--...__    |
                       (|  `\       __-\|
                       ||    \_   /~    |
                       |)     \~-'      |
                        |      | \      '
                        |      |  \    :
                         \     |  |    |
                          |    )  (    )
                           \  /;  /\  |
                           |    |/   |
                           |    |   |
                            \  .'  ||
                            |  |  | |
                            (  | |  |
                            |   \ \ |
                            || o `.)|
                            |`\\\\) |
                            |       |
                            |       |
                            |       |
*/
vector < int > a,z,p;
int f(int x) {
    if (p[x] == x) return x;
    else return p[x] = f(p[x]);
}
void solve(int x,int y) {
    x = f(x);
    y = f(y);
    if (z[y] > z[x]) {
        p[x] = y;
        z[y]+=z[x];
    } else {
        p[y] = x;
        z[x]+=z[y];
    }
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,m;
    cin >> n >> m;
    a.resize(n);
    p.resize(n);
    z.resize(n);
    fori(n) cin >> a[i];
    fori(n) a[i]--;
    fori(n) p[i] = i;
    fori(n) z[i] = 1;
    fori(m) {
        int x,y;
        cin >> x >> y;
        x--; y--;
        solve(x,y);
    }
    vector < set < int > > kek;
    kek.resize(n);
    fori(n) {
        kek[f(i)].insert(a[i]);
    }
    int ans = 0;
    fori(n) {
        int t = f(i);
        if (kek[t].find(i) != kek[t].end()) ans++;
    }
    cout << ans;
}














Measured memory_bytes: 10752