#include using namespace std; struct UnionFind { vector parent, rank, siz; UnionFind(int n) { parent.resize(n, -1); rank.resize(n, 1); siz.resize(n, 1); } int find(int x) { if (parent[x] == -1) { return x; } else { return parent[x] = find(parent[x]); } } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) { return; } if (rank[x] < rank[y]) { swap(x, y); } parent[y] = x; if (rank[x] == rank[y]) { rank[x]++; } siz[x] += siz[y]; } bool same(int x, int y) { return find(x) == find(y); } int size(int x) { return siz[find(x)]; } }; int main() { int N, M; cin >> N >> M; vector C(N); for (int i = 0; i < N; i++) { cin >> C[i]; C[i]--; } UnionFind uf(N); for (int i = 0; i < M; i++) { int u, v; cin >> u >> v; u--; v--; if (C[u] == C[v]) uf.unite(u, v); } int ans = 0; vector> g(N); for (int i = 0; i < N; i++) { g[C[i]].insert(uf.find(i)); } for (auto i : g) { if (i.size() > 0) ans += i.size() - 1; } cout << ans << endl; }