// Copyright (C) 2018 Sayutin Dmitry.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; version 3

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; If not, see <http://www.gnu.org/licenses/>.

#include <iostream>
#include <vector>
#include <stdint.h>
#include <algorithm>
#include <set>
#include <map>
#include <array>
#include <queue>
#include <stack>
#include <functional>
#include <utility>
#include <string>
#include <assert.h>
#include <iterator>
#include <cstdint>
#include <cinttypes>
#include <string.h>
#include <random>
#include <numeric>
#include <tuple>


using std::cin;
using std::cout;
using std::cerr;

using std::vector;
using std::map;
using std::array;
using std::set;
using std::string;

using std::pair;
using std::make_pair;

using std::min;
using std::abs;
using std::max;

using std::unique;
using std::sort;
using std::generate;
using std::reverse;
using std::min_element;
using std::max_element;

#ifdef LOCAL
#define LASSERT(X) assert(X)
#else
#define LASSERT(X) {}
#endif

template <typename T>
T input() {
    T res;
    cin >> res;
    LASSERT(cin);
    return res;
}

template <typename IT>
void input_seq(IT b, IT e) {
    std::generate(b, e, input<typename std::remove_reference<decltype(*b)>::type>);
}

#define SZ(vec)         int((vec).size())
#define ALL(data)       data.begin(),data.end()
#define RALL(data)      data.rbegin(),data.rend()
#define TYPEMAX(type)   std::numeric_limits<type>::max()
#define TYPEMIN(type)   std::numeric_limits<type>::min()

#define pb push_back
#define eb emplace_back

void zhfs(vector<vector<int>>& graph, vector<char>& used, vector<int>& perm, vector<int>& lst_index, vector<int>& lst_vals, int v) {
    lst_index.push_back(v);
    lst_vals.push_back(perm[v]);

    used[v] = 1;

    for (int u: graph[v])
        if (not used[u])
            zhfs(graph, used, perm, lst_index, lst_vals, u);
}

int main() {
    std::iostream::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    
    // code here.
    int n = input<int>();
    int m = input<int>();

    vector<int> p(n);
    for (int& elem: p)
        elem = input<int>() - 1;

    vector<vector<int>> graph(n);

    for (int i = 0; i != m; ++i) {
        int v = input<int>() - 1;
        int u = input<int>() - 1;

        graph[v].push_back(u);
        graph[u].push_back(v);
    }

    vector<char> used(n, false);
    int ans = 0;
    for (int i = 0; i != n; ++i)
        if (not used[i]) {
            vector<int> lst_index, lst_vals;

            zhfs(graph, used, p, lst_index, lst_vals, i);

            std::sort(ALL(lst_index));
            std::sort(ALL(lst_vals));

            vector<int> blad(SZ(lst_index));
            ans += set_intersection(ALL(lst_index), ALL(lst_vals), blad.begin()) - blad.begin();
        }

    cout << ans << "\n";
    
    
    return 0;
}


Measured memory_bytes: 12660