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

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

xys = [tuple(int(x)-1 for x in input().split()) for _ in range(m)]



nodes = list(range(n))

union = {node:node for node in nodes}



def root(node, previouss=None):

    if not previouss:

        previouss = []



    if union[node] == node:

        for previous in previouss:

            union[previous] = node

        return node

    else:

        previouss.append(node)

        return root(union[node], previouss)



for x, y in xys:

    rx = root(x)

    ry = root(y)

    union[rx] = ry



groups = {node:{node} for node, rootn in list(union.items()) if node==rootn} 

for node in nodes:

    if node in groups:

        continue

    group = groups[root(node)]

    group.add(node)

    groups[node] = group



print((sum(p in groups[i] for i, p in enumerate(ps))))
n, m = list(map(int, input().split()))

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

xys = [tuple(int(x)-1 for x in input().split()) for _ in range(m)]



nodes = list(range(n))

union = {node:node for node in nodes}



def root(node):

    if union[node] == node:

        return node

    union[node] = root(union[node])

    return union[node]



for x, y in xys:

    rx = root(x)

    ry = root(y)

    union[rx] = ry



groups = {node:{node} for node, rootn in list(union.items()) if node==rootn} 

for node in nodes:

    if node in groups:

        continue

    group = groups[root(node)]

    group.add(node)

    groups[node] = group



print((sum(p in groups[i] for i, p in enumerate(ps))))
n, m = list(map(int, input().split()))
ps = [int(p) - 1 for p in input().split()]
xys = [tuple(int(x) - 1 for x in input().split()) for _ in range(m)]
nodes = list(range(n))
union = {node: node for node in nodes}


def root(node, previouss=None):
    if not previouss:
        previouss = []
    if union[node] == node:
        for previous in previouss:
            union[previous] = node
        return node
    else:
        previouss.append(node)
        return root(union[node], previouss)


for x, y in xys:
    rx = root(x)
    ry = root(y)
    union[rx] = ry
groups = {node: {node} for node, rootn in list(union.items()) if node == rootn}
for node in nodes:
    if node in groups:
        continue
    group = groups[root(node)]
    group.add(node)
    groups[node] = group
print((sum(p in groups[i] for i, p in enumerate(ps))))

n, m = list(map(int, input().split()))
ps = [int(p) - 1 for p in input().split()]
xys = [tuple(int(x) - 1 for x in input().split()) for _ in range(m)]
nodes = list(range(n))
union = {node: node for node in nodes}


def root(node):
    if union[node] == node:
        return node
    union[node] = root(union[node])
    return union[node]


for x, y in xys:
    rx = root(x)
    ry = root(y)
    union[rx] = ry
groups = {node: {node} for node, rootn in list(union.items()) if node == rootn}
for node in nodes:
    if node in groups:
        continue
    group = groups[root(node)]
    group.add(node)
    groups[node] = group
print((sum(p in groups[i] for i, p in enumerate(ps))))

-def root(node, previouss=None): -    if not previouss: -        previouss = [] +def root(node): -        for previous in previouss: -            union[previous] = node -    else: -        previouss.append(node) -        return root(union[node], previouss) +    union[node] = root(union[node]) +    return union[node]