import queue

N,M=list(map(int,input().split()))

p=[int(i)-1 for i in input().split()]

x=[0 for i in range(M)]

y=[0 for i in range(M)]

edge=[set() for i in range(N)]

for i in range(M):

    x[i],y[i]=list(map(int,input().split()))

    x[i]-=1;y[i]-=1

    edge[x[i]].add(y[i])

    edge[y[i]].add(x[i])

k=0

L=[0 for i in range(N)]

q=queue.Queue()

reached=[0 for i in range(N)]

for i in  range(N):

    if reached[i]==1:

        continue

    k+=1

    q.put(i)

    reached[i]=1

    L[i]=k

    while(not(q.empty())):

        r=q.get()

        for v in edge[r]:

            if not(reached[v]):

                q.put(v)

                reached[v]=1

                L[v]=k

ans=0

for i in range(N):

    if L[i]==L[p[i]]:

        ans+=1

print(ans)

N,M=list(map(int,input().split()))

par=[0 for i in range(N)]

rnk=[0 for i in range(N)]

def init(n):

    for i in range(N):

        par[i]=i

        rnk[i]=0

def find(x):

    if par[x]==x:

        return x

    else:

        par[x]=find(par[x])

        return par[x]

def unite(x,y):

    x=find(x)

    y=find(y)

    if x==y:

        return

    if (rnk[x]<rnk[y]):

        par[x]=y

    else:

        par[y]=x

        if(rnk[x]==rnk[y]):

            rnk[x]+=1

def same(x,y):

    return find(x)==find(y)



p=[int(i)-1 for i in input().split()]

init(N)

for i in range(M):

    x,y=list(map(int,input().split()))

    unite(x-1,y-1)

ans=0

for i in range(N):

    if same(i,p[i]):

        ans+=1

print(ans)

import queue

N, M = list(map(int, input().split()))
p = [int(i) - 1 for i in input().split()]
x = [0 for i in range(M)]
y = [0 for i in range(M)]
edge = [set() for i in range(N)]
for i in range(M):
    x[i], y[i] = list(map(int, input().split()))
    x[i] -= 1
    y[i] -= 1
    edge[x[i]].add(y[i])
    edge[y[i]].add(x[i])
k = 0
L = [0 for i in range(N)]
q = queue.Queue()
reached = [0 for i in range(N)]
for i in range(N):
    if reached[i] == 1:
        continue
    k += 1
    q.put(i)
    reached[i] = 1
    L[i] = k
    while not (q.empty()):
        r = q.get()
        for v in edge[r]:
            if not (reached[v]):
                q.put(v)
                reached[v] = 1
                L[v] = k
ans = 0
for i in range(N):
    if L[i] == L[p[i]]:
        ans += 1
print(ans)

N, M = list(map(int, input().split()))
par = [0 for i in range(N)]
rnk = [0 for i in range(N)]


def init(n):
    for i in range(N):
        par[i] = i
        rnk[i] = 0


def find(x):
    if par[x] == x:
        return x
    else:
        par[x] = find(par[x])
        return par[x]


def unite(x, y):
    x = find(x)
    y = find(y)
    if x == y:
        return
    if rnk[x] < rnk[y]:
        par[x] = y
    else:
        par[y] = x
        if rnk[x] == rnk[y]:
            rnk[x] += 1


def same(x, y):
    return find(x) == find(y)


p = [int(i) - 1 for i in input().split()]
init(N)
for i in range(M):
    x, y = list(map(int, input().split()))
    unite(x - 1, y - 1)
ans = 0
for i in range(N):
    if same(i, p[i]):
        ans += 1
print(ans)

-import queue +N, M = list(map(int, input().split())) +par = [0 for i in range(N)] +rnk = [0 for i in range(N)] -N, M = list(map(int, input().split())) + +def init(n): +    for i in range(N): +        par[i] = i +        rnk[i] = 0 + + +def find(x): +    if par[x] == x: +        return x +    else: +        par[x] = find(par[x]) +        return par[x] + + +def unite(x, y): +    x = find(x) +    y = find(y) +    if x == y: +        return +    if rnk[x] < rnk[y]: +        par[x] = y +    else: +        par[y] = x +        if rnk[x] == rnk[y]: +            rnk[x] += 1 + + +def same(x, y): +    return find(x) == find(y) + + -x = [0 for i in range(M)] -y = [0 for i in range(M)] -edge = [set() for i in range(N)] +init(N) -    x[i], y[i] = list(map(int, input().split())) -    x[i] -= 1 -    y[i] -= 1 -    edge[x[i]].add(y[i]) -    edge[y[i]].add(x[i]) -k = 0 -L = [0 for i in range(N)] -q = queue.Queue() -reached = [0 for i in range(N)] -for i in range(N): -    if reached[i] == 1: -        continue -    k += 1 -    q.put(i) -    reached[i] = 1 -    L[i] = k -    while not (q.empty()): -        r = q.get() -        for v in edge[r]: -            if not (reached[v]): -                q.put(v) -                reached[v] = 1 -                L[v] = k +    x, y = list(map(int, input().split())) +    unite(x - 1, y - 1) -    if L[i] == L[p[i]]: +    if same(i, p[i]):