#include using namespace std; #define rep(i, n) for( ll i = 0; i < n; i++ ) using ll = long long; struct UnionFind { vector data; UnionFind(int size) : data(size, -1) { } bool unionSet(int x, int y) { x = root(x); y = root(y); if (x != y) { if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } return x != y; } bool findSet(int x, int y) { return root(x) == root(y); } int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); } int size(int x) { return -data[root(x)]; } }; int main() { int N, M; cin >> N >> M; vector c( N ); vector> vc( N ); rep(i, N) { cin >> c[i]; c[i]--; vc[c[i]].push_back( i ); } UnionFind uf( N ); rep(i, M) { int a, b; cin >> a >> b; a--; b--; if(c[a] == c[b]) uf.unionSet(a, b); } int ans = 0; rep(i, N) { if(vc[i].size() < 2) continue; for( int e : vc[i] ) { if(uf.findSet(vc[i][0], e)) continue; ans++; uf.unionSet(vc[i][0], e); } } cout << ans << endl; }