def main():

    cnt = 0

    for _ in range(m):

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

        unite(x, y)

    for i in range(1, n+1):

        if same(i, p[i]):

            cnt += 1

    print(cnt)

        

def root(x):

    if par[x] == x:

        return x

    par[x] = root(par[x])

    return par[x]



def same(x, y):

    rx = root(x)

    ry = root(y)

    return rx == ry



def unite(x, y):

    x = root(x)

    y = root(y)

    if x != y:

        par[x] = y



if __name__ == "__main__":

    n, m = list(map(int, input().split()))

    p = [0] + list(map(int, input().split()))

    par = list(range(n+1))

    main()

def main():

    cnt = 0

    for _ in range(m):

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

        unite(x, y)

    for i in range(1, n+1):

        if same(i, p[i]):

            cnt += 1

    print(cnt)

        

def root(x):

    if parent[x] == x:

        return x

    parent[x] = root(parent[x])

    return parent[x]



def same(x, y):

    return root(x) == root(y)



def unite(x, y):

    x = root(x)

    y = root(y)

    if x != y:

        if rank[x] < rank[y]:

            parent[x] = y

        elif rank[x] > rank[y]:

            parent[y] = x

        else:

            parent[y] = x

            rank[x] += 1



if __name__ == "__main__":

    n, m = list(map(int, input().split()))

    p = [0] + list(map(int, input().split()))

    parent = list(range(n+1))

    rank = [0]*(n+1)

    main()

def main():
    cnt = 0
    for _ in range(m):
        x, y = list(map(int, input().split()))
        unite(x, y)
    for i in range(1, n + 1):
        if same(i, p[i]):
            cnt += 1
    print(cnt)


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


def same(x, y):
    rx = root(x)
    ry = root(y)
    return rx == ry


def unite(x, y):
    x = root(x)
    y = root(y)
    if x != y:
        par[x] = y


if __name__ == "__main__":
    n, m = list(map(int, input().split()))
    p = [0] + list(map(int, input().split()))
    par = list(range(n + 1))
    main()

def main():
    cnt = 0
    for _ in range(m):
        x, y = list(map(int, input().split()))
        unite(x, y)
    for i in range(1, n + 1):
        if same(i, p[i]):
            cnt += 1
    print(cnt)


def root(x):
    if parent[x] == x:
        return x
    parent[x] = root(parent[x])
    return parent[x]


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


def unite(x, y):
    x = root(x)
    y = root(y)
    if x != y:
        if rank[x] < rank[y]:
            parent[x] = y
        elif rank[x] > rank[y]:
            parent[y] = x
        else:
            parent[y] = x
            rank[x] += 1


if __name__ == "__main__":
    n, m = list(map(int, input().split()))
    p = [0] + list(map(int, input().split()))
    parent = list(range(n + 1))
    rank = [0] * (n + 1)
    main()

-    if par[x] == x: +    if parent[x] == x: -    par[x] = root(par[x]) -    return par[x] +    parent[x] = root(parent[x]) +    return parent[x] -    rx = root(x) -    ry = root(y) -    return rx == ry +    return root(x) == root(y) -        par[x] = y +        if rank[x] < rank[y]: +            parent[x] = y +        elif rank[x] > rank[y]: +            parent[y] = x +        else: +            parent[y] = x +            rank[x] += 1 -    par = list(range(n + 1)) +    parent = list(range(n + 1)) +    rank = [0] * (n + 1)