def root(v):

    if v == par[v]:

        return v

    par[v] = root(par[v])

    return par[v]



def unite(u, v):

    u = root(u)

    v = root(v)

    if u == v:

        return

    if rank[u] < rank[v]:

        par[u] = v

    else:

        par[v] = u

        if rank[u] == rank[v]:

            rank[v] += 1



def same(u, v):

    u = root(u)

    v = root(v)

    if u == v:

        return 1

    return 0



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

P = list(map(int, input().split()))

par = list(range(N))

rank = [0] * N

for _ in range(M):

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

    unite(x-1, y-1)



ans = 0

for i in range(N):

    ans += same(i, P[i] - 1)



print(ans)



import sys

sys.setrecursionlimit(10 ** 6)

input = sys.stdin.readline



def root(v):

    if v == par[v]:

        return v

    par[v] = root(par[v])

    return par[v]



def unite(u, v):

    u = root(u)

    v = root(v)

    if u == v:

        return

    if rank[u] < rank[v]:

        u, v = v, u

    par[v] = u

    if rank[u] == rank[v]:

        rank[u] += 1



def connected(u, v):

    return 1 if root(u) == root(v) else 0



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

P = list(map(int, input().split()))

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

rank = [0] * N

for _ in range(M):

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

    unite(x-1, y-1)

res = 0

for i in range(N):

    res += connected(i, P[i]-1)

print(res)

def root(v):
    if v == par[v]:
        return v
    par[v] = root(par[v])
    return par[v]


def unite(u, v):
    u = root(u)
    v = root(v)
    if u == v:
        return
    if rank[u] < rank[v]:
        par[u] = v
    else:
        par[v] = u
        if rank[u] == rank[v]:
            rank[v] += 1


def same(u, v):
    u = root(u)
    v = root(v)
    if u == v:
        return 1
    return 0


N, M = list(map(int, input().split()))
P = list(map(int, input().split()))
par = list(range(N))
rank = [0] * N
for _ in range(M):
    x, y = list(map(int, input().split()))
    unite(x - 1, y - 1)
ans = 0
for i in range(N):
    ans += same(i, P[i] - 1)
print(ans)

import sys

sys.setrecursionlimit(10**6)
input = sys.stdin.readline


def root(v):
    if v == par[v]:
        return v
    par[v] = root(par[v])
    return par[v]


def unite(u, v):
    u = root(u)
    v = root(v)
    if u == v:
        return
    if rank[u] < rank[v]:
        u, v = v, u
    par[v] = u
    if rank[u] == rank[v]:
        rank[u] += 1


def connected(u, v):
    return 1 if root(u) == root(v) else 0


N, M = list(map(int, input().split()))
P = list(map(int, input().split()))
par = [i for i in range(N)]
rank = [0] * N
for _ in range(M):
    x, y = list(map(int, input().split()))
    unite(x - 1, y - 1)
res = 0
for i in range(N):
    res += connected(i, P[i] - 1)
print(res)

+import sys + +sys.setrecursionlimit(10**6) +input = sys.stdin.readline + + -        par[u] = v -    else: -        par[v] = u -        if rank[u] == rank[v]: -            rank[v] += 1 +        u, v = v, u +    par[v] = u +    if rank[u] == rank[v]: +        rank[u] += 1 -def same(u, v): -    u = root(u) -    v = root(v) -    if u == v: -        return 1 -    return 0 +def connected(u, v): +    return 1 if root(u) == root(v) else 0 -par = list(range(N)) +par = [i for i in range(N)] -ans = 0 +res = 0 -    ans += same(i, P[i] - 1) -print(ans) +    res += connected(i, P[i] - 1) +print(res)