# ARC097D - Equals (ABC097D)

import sys



input = sys.stdin.readline

sys.setrecursionlimit(10 ** 9)





def dfs(v: int, color: int) -> None:

    seen[v] = 1  # make vertex v searched

    group[v] = color

    for u in G[v]:  # search vertices available from v recursively

        if not seen[u]:  # skip if already searched

            dfs(u, color)





def main():

    # check i and Pi are connected components or not

    global G, seen, group

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

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

    E = tuple(tuple(map(int, input().split())) for _ in range(M))

    G = [[] for _ in range(N + 1)]  # construct a graph (1-idx)

    for v, u in E:

        G[v] += [u]

        G[u] += [v]

    seen, group = [0] * (N + 1), [0] * (N + 1)

    color = 1

    for i in range(1, N + 1):  # connected components have the same color

        if not seen[i]:

            dfs(i, color)

            color += 1

    ans = sum(group[i] == group[p] for i, p in enumerate(P, 1))

    print(ans)





if __name__ == "__main__":

    main()
# ARC097D - Equals

import sys



input = sys.stdin.readline

sys.setrecursionlimit(10 ** 9)





def dfs(v: int, color: int) -> None:

    group[v] = color  # make vertex v searched

    for u in G[v]:  # search vertices available from v recursively

        if not group[u]:  # skip if already searched

            dfs(u, color)





def main():

    # check i and Pi are connected components or not

    global G, group

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

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

    E = tuple(tuple(map(int, input().split())) for _ in range(M))

    G = [[] for _ in range(N + 1)]  # construct a graph (1-idx)

    for v, u in E:

        G[v] += [u]

        G[u] += [v]

    group = [0] * (N + 1)

    color = 1

    for v in range(1, N + 1):  # connected components are in the same group

        if not group[v]:

            dfs(v, color)

            color += 1

    ans = sum(group[i] == group[p] for i, p in enumerate(P, 1))

    print(ans)





if __name__ == "__main__":

    main()
# ARC097D - Equals (ABC097D)
import sys

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


def dfs(v: int, color: int) -> None:
    seen[v] = 1  # make vertex v searched
    group[v] = color
    for u in G[v]:  # search vertices available from v recursively
        if not seen[u]:  # skip if already searched
            dfs(u, color)


def main():
    # check i and Pi are connected components or not
    global G, seen, group
    N, M = tuple(map(int, input().split()))
    P = tuple(map(int, input().split()))
    E = tuple(tuple(map(int, input().split())) for _ in range(M))
    G = [[] for _ in range(N + 1)]  # construct a graph (1-idx)
    for v, u in E:
        G[v] += [u]
        G[u] += [v]
    seen, group = [0] * (N + 1), [0] * (N + 1)
    color = 1
    for i in range(1, N + 1):  # connected components have the same color
        if not seen[i]:
            dfs(i, color)
            color += 1
    ans = sum(group[i] == group[p] for i, p in enumerate(P, 1))
    print(ans)


if __name__ == "__main__":
    main()

# ARC097D - Equals
import sys

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


def dfs(v: int, color: int) -> None:
    group[v] = color  # make vertex v searched
    for u in G[v]:  # search vertices available from v recursively
        if not group[u]:  # skip if already searched
            dfs(u, color)


def main():
    # check i and Pi are connected components or not
    global G, group
    N, M = tuple(map(int, input().split()))
    P = tuple(map(int, input().split()))
    E = tuple(tuple(map(int, input().split())) for _ in range(M))
    G = [[] for _ in range(N + 1)]  # construct a graph (1-idx)
    for v, u in E:
        G[v] += [u]
        G[u] += [v]
    group = [0] * (N + 1)
    color = 1
    for v in range(1, N + 1):  # connected components are in the same group
        if not group[v]:
            dfs(v, color)
            color += 1
    ans = sum(group[i] == group[p] for i, p in enumerate(P, 1))
    print(ans)


if __name__ == "__main__":
    main()

-# ARC097D - Equals (ABC097D) +# ARC097D - Equals -    seen[v] = 1  # make vertex v searched -    group[v] = color +    group[v] = color  # make vertex v searched -        if not seen[u]:  # skip if already searched +        if not group[u]:  # skip if already searched -    global G, seen, group +    global G, group -    seen, group = [0] * (N + 1), [0] * (N + 1) +    group = [0] * (N + 1) -    for i in range(1, N + 1):  # connected components have the same color -        if not seen[i]: -            dfs(i, color) +    for v in range(1, N + 1):  # connected components are in the same group +        if not group[v]: +            dfs(v, color)