#include using namespace std; #define rep(i,n) for (int i = 0; i < (n); i++) template inline bool chmax(T &a, T b) {return ((a < b) ? (a = b, true) : (false));} template inline bool chmin(T &a, T b) {return ((a > b) ? (a = b, true) : (false));} typedef long long ll; typedef pair P; const int MAX_N = 500005; int par[MAX_N]; int dep[MAX_N]; void init(int n) { for(int i = 0; i < n; i++) { par[i] = i; dep[i] = 0; } } int find(int x) { if(par[x] == x) { return x; } else { return par[x] = find(par[x]); } } void unite(int x, int y) { x = find(x); y = find(y); if(x == y) return; if(dep[x] < dep[y]) { par[x] = y; } else { par[y] = x; if(dep[x] == dep[y]) dep[x]++; } } bool same(int x, int y) { return find(x) == find(y); } vector c; vector> to; vector vis; void dfs(int i, int p) { vis[i] = true; rep(j,to[i].size()) { int v = to[i][j]; if(v == p) { continue; } if(c[v] == c[i]) { unite(v, i); dfs(v, i); } } } int main() { int n, m; cin >> n >> m; c.resize(n); rep(i,n) cin >> c[i]; vector u(m), v(m); rep(i,m) cin >> u[i] >> v[i]; rep(i,n) { c[i]--; } to.resize(n); vis.resize(n); rep(i,m) { u[i]--; v[i]--; to[u[i]].push_back(v[i]); to[v[i]].push_back(u[i]); } init(n); rep(i,n) { if(!vis[i]) { dfs(i, -1); } } vector> kind(n); rep(i,n) { kind[c[i]].push_back(i); } int ans = 0; rep(i,n) { set st; rep(j,kind[i].size()) { st.insert(find(kind[i][j])); } if(st.size() > 0) { ans += st.size() - 1; } } cout << ans << endl; return 0; }