class UnionFind: def __init__(self, N): self.p = [-1] * N def find(self, x): y = self.p[x] while y >= 0: x = y y = self.p[y] return x def unite(self, x, y): x, y = self.find(x), self.find(y) if x == y: return if -self.p[x] < -self.p[y]: x, y = y, x self.p[x] += self.p[y] self.p[y] = x N, M = map(int, input().split()) uf = UnionFind(N) ans = 0 C = list(map(int, input().split())) for _ in range(M): v, w = map(int, input().split()) v -= 1 w -= 1 if C[v] != C[w]: continue uf.unite(v, w) d = {} for i, c in enumerate(C): if c not in d: d[c] = [] d[c].append(i) for c in d: S = set() for i in d[c]: S.add(uf.find(i)) ans += len(S) - 1 print(ans)