#緑以下G #UnionFind class UnionFind: def __init__(self,N): self._parent=[-1 for i in[0]*N] def find(self,v): #頂点vの親を探し、経路圧縮する vertices=[] while self._parent[v]>=0: vertices.append(v);v=self._parent[v] for i in vertices: self._parent[i]=v return v def unite(self,x,y): #頂点xとyを併合し、併合の有無を返す x,y = self.find(x),self.find(y) if x==y: return 0 if self._parent[x]>self._parent[y]: x,y=y,x #負値で管理 self._parent[x]+=self._parent[y]; self._parent[y]=x; return 1 def same(self,x,y):return self.find(x)==self.find(y) #xとyは同一集合か返す def size(self,x): return -self._parent[self.find(x)] #xの集合のサイズを求める from collections import defaultdict #入力受取 N,M = map(int,input().split()) C = list(map(int,input().split())) UF = UnionFind(N) for _ in range(M): u,v = map(lambda x: int(x)-1,input().split()) if C[u] == C[v]: UF.unite(u,v) #色ごとに連結判定 D = defaultdict(list) for i,c in enumerate(C): D[c].append(i) ans = 0 for c in D.keys(): base = D[c][0] for i in range(1,len(D[c])): if not UF.same(base,i): UF.unite(base,i); ans += 1 print(ans)