from collections import defaultdict class UnionFind(): def __init__(self, n): self.n = n self.root = [-1] * n self.rank = [0] * n def find(self, x): if self.root[x] < 0: return x else: self.root[x] = self.find(self.root[x]) return self.root[x] def unite(self, x, y): x = self.find(x) y = self.find(y) if x == y: return elif self.rank[x] > self.rank[y]: self.root[x] += self.root[y] self.root[y] = x else: self.root[y] += self.root[x] self.root[x] = y if self.rank[x] == self.rank[y]: self.rank[y] += 1 def same(self, x, y): return self.find(x) == self.find(y) n, m = map(int, input().split()) cl = list(map(int, input().split())) d = defaultdict(list) for i, c in enumerate(cl): d[c].append(i) uf = UnionFind(n) for _ in range(m): u, v = map(int, input().split()) u, v = u - 1, v - 1 if cl[u] == cl[v]: uf.unite(u, v) ans = 0 for k in d.keys(): l = d[k] for a, b in zip(l, l[1:]): if uf.same(a, b): continue uf.unite(a, b) ans += 1 print(ans)