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

pn = list([int(x)-1 for x in input().split()])

ls = [-1] * n

for i in pn:

    ls[pn[i]] = i

#print(ls)



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



def find(x):

    if par[x] == x:

        return x

    else:

        s = find(par[x])

        par[x] = s

        return s



def unite(x,y):

    s = find(x)

    t = find(y)

    if s>t:

        par[s] = t

    else:

        par[t] = s





for _ in range(m):

    x, y = [int(x)-1 for x in input().split()]

    unite(ls[x],ls[y])



dic = {}

for i in range(n):

    a = find(i)

    if a in dic:

        dic[a].add(ls[i])

    else:

        dic[a] = set([ls[i]])

#print(dic)

#print(par)

ans = 0

for i in range(n):

    if i in dic[find(i)]:

       ans+=1

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

pn = list([int(x)-1 for x in input().split()])

ls = [-1] * n

for i in pn:

    ls[pn[i]] = i

#print(ls)



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



def find(x):

    if par[x] == x:

        return x

    else:

        s = find(par[x])

        par[x] = s

        return s



def unite(x,y):

    s = find(x)

    t = find(y)

    if s>t:

        par[s] = t

    else:

        par[t] = s





for _ in range(m):

    x, y = [int(x)-1 for x in input().split()]

    unite(ls[x],ls[y])





ans2 = 0

for i in range(n): # i番目の数字がいる場所の親とiの場所

    place1 = ls[pn[i]]

    place2 = ls[i]



    if find(place1)==find(place2):

       ans2+=1

print(ans2)
n, m = list(map(int, input().split()))
pn = list([int(x) - 1 for x in input().split()])
ls = [-1] * n
for i in pn:
    ls[pn[i]] = i
# print(ls)
par = [i for i in range(n)]


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


def unite(x, y):
    s = find(x)
    t = find(y)
    if s > t:
        par[s] = t
    else:
        par[t] = s


for _ in range(m):
    x, y = [int(x) - 1 for x in input().split()]
    unite(ls[x], ls[y])
dic = {}
for i in range(n):
    a = find(i)
    if a in dic:
        dic[a].add(ls[i])
    else:
        dic[a] = set([ls[i]])
# print(dic)
# print(par)
ans = 0
for i in range(n):
    if i in dic[find(i)]:
        ans += 1
print(ans)

n, m = list(map(int, input().split()))
pn = list([int(x) - 1 for x in input().split()])
ls = [-1] * n
for i in pn:
    ls[pn[i]] = i
# print(ls)
par = [i for i in range(n)]


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


def unite(x, y):
    s = find(x)
    t = find(y)
    if s > t:
        par[s] = t
    else:
        par[t] = s


for _ in range(m):
    x, y = [int(x) - 1 for x in input().split()]
    unite(ls[x], ls[y])
ans2 = 0
for i in range(n):  # i番目の数字がいる場所の親とiの場所
    place1 = ls[pn[i]]
    place2 = ls[i]
    if find(place1) == find(place2):
        ans2 += 1
print(ans2)

-dic = {} -for i in range(n): -    a = find(i) -    if a in dic: -        dic[a].add(ls[i]) -    else: -        dic[a] = set([ls[i]]) -# print(dic) -# print(par) -ans = 0 -for i in range(n): -    if i in dic[find(i)]: -        ans += 1 -print(ans) +ans2 = 0 +for i in range(n):  # i番目の数字がいる場所の親とiの場所 +    place1 = ls[pn[i]] +    place2 = ls[i] +    if find(place1) == find(place2): +        ans2 += 1 +print(ans2)