Buggy python submission (verdict: Runtime Error):
(n, m) = list(map(int, input().split()))
p = list(map(int, input().split()))
cG = []
for i in range(0, m):
    e = list(map(int, input().split()))
    do = []
    for c in cG:
        if e[0] in c or e[1] in c:
            if not do == []:
                mix = do + c
                cG.remove(do)
                cG.remove(c)
                cG.append(mix)
                break
            else:
                do = c
        if e[0] in c:
            c.append(e[1])
        elif e[1] in c:
            c.append(e[0])
    if len(do) == 0:
        cG.append(e)

res = 0
bebe = [-1] * n
for i, c in enumerate(cG):
    for e in c:
        bebe[e - 1] = i

for i in range(n):
    if bebe[i] == bebe[p[i] - 1]:
        re += 1

print(res)


Fixed submission:
def ge(x):
    return int(x) - 1


(n, m) = tuple(map(int, input().split()))
inp = list(map(ge, input().split()))
arr = [i for i in range(n)]


def root(a):
    global arr
    if arr[a] == a:
        return a
    else:
        tmp = root(arr[a])
        arr[a] = tmp
        return tmp


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


def merge(x, y):
    global arr
    if not sameroot(x, y):
        arr[root(y)] = root(x)


for i in range(m):
    merge(*tuple(map(ge, input().split())))

res = 0
for i in range(n):
    if sameroot(i, inp[i]):
        res += 1
print(res)

Buggy python submission (verdict: Runtime Error):
(n, m) = list(map(int, input().split()))
p = list(map(int, input().split()))
cG = []
for i in range(0, m):
    e = list(map(int, input().split()))
    do = []
    for c in cG:
        if e[0] in c or e[1] in c:
            if not do == []:
                mix = do + c
                cG.remove(do)
                cG.remove(c)
                cG.append(mix)
                break
            else:
                do = c
        if e[0] in c:
            c.append(e[1])
        elif e[1] in c:
            c.append(e[0])
    if len(do) == 0:
        cG.append(e)

res = 0
bebe = [-1] * n
for c, i in enumerate(cG):
    for e in c:
        bebe[e - 1] = i

for i in range(n):
    if bebe[i] == bebe[p[i] - 1]:
        re += 1

print(res)


Fixed submission:
def ge(x):
    return int(x) - 1


(n, m) = tuple(map(int, input().split()))
inp = list(map(ge, input().split()))
arr = [i for i in range(n)]


def root(a):
    global arr
    if arr[a] == a:
        return a
    else:
        tmp = root(arr[a])
        arr[a] = tmp
        return tmp


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


def merge(x, y):
    global arr
    if not sameroot(x, y):
        arr[root(y)] = root(x)


for i in range(m):
    merge(*tuple(map(ge, input().split())))

res = 0
for i in range(n):
    if sameroot(i, inp[i]):
        res += 1
print(res)

Buggy python submission (verdict: Time Limit Exceeded):
(n, m) = list(map(int, input().split()))
p = list(map(int, input().split()))
cG = []
cGT = []
for i in range(0, m):
    e = list(map(int, input().split()))
    aru = False
    do = []
    for c in cG:
        if e[0] in c or e[1] in c:
            if not do == []:
                mix = do + c
                cG.remove(do)
                cG.remove(c)
                cG.append(mix)
                break
            else:
                do = c
        if e[0] in c:
            aru = True
            c.append(e[1])
            cGT.append(e[1])
        elif e[1] in c:
            aru = True
            c.append(e[0])
            cGT.append(e[0])
    if not aru:
        cG.append(e)
        cGT = cGT + e

cGT = set(cGT)
res = 0
for i in list(set(p) - cGT):
    if p[i - 1] == i:
        res = res + 1

for i in cGT:
    for c in cG:
        if i in c:
            if p.index(i) + 1 in c:
                res = res + 1
                break
print(res)


Fixed submission:
def ge(x):
    return int(x) - 1


(n, m) = tuple(map(int, input().split()))
inp = list(map(ge, input().split()))
arr = [i for i in range(n)]


def root(a):
    global arr
    if arr[a] == a:
        return a
    else:
        tmp = root(arr[a])
        arr[a] = tmp
        return tmp


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


def merge(x, y):
    global arr
    if not sameroot(x, y):
        arr[root(y)] = root(x)


for i in range(m):
    merge(*tuple(map(ge, input().split())))

res = 0
for i in range(n):
    if sameroot(i, inp[i]):
        res += 1
print(res)

Buggy python submission (verdict: Time Limit Exceeded):
(n, m) = list(map(int, input().split()))
p = list(map(int, input().split()))
cG = []
cGT = []
for i in range(0, m):
    e = list(map(int, input().split()))
    aru = False
    do = []
    for c in cG:
        if e[0] in c or e[1] in c:
            if not do == []:
                mix = do + c
                cG.remove(do)
                cG.remove(c)
                cG.append(mix)
                break
            else:
                do = c
        if e[0] in c:
            aru = True
            c.append(e[1])
            cGT.append(e[1])
        elif e[1] in c:
            aru = True
            c.append(e[0])
            cGT.append(e[0])
    if not aru:
        cG.append(e)
        cGT = cGT + e

cGT = set(cGT)
cant = list(set(p) - cGT)
res = 0
for i in cant:
    if p[i - 1] == i:
        res = res + 1

for i in cGT:
    for c in cG:
        if i in c:
            ku = p.index(i) + 1
            if ku in c:
                res = res + 1
print(res)


Fixed submission:
def ge(x):
    return int(x) - 1


(n, m) = tuple(map(int, input().split()))
inp = list(map(ge, input().split()))
arr = [i for i in range(n)]


def root(a):
    global arr
    if arr[a] == a:
        return a
    else:
        tmp = root(arr[a])
        arr[a] = tmp
        return tmp


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


def merge(x, y):
    global arr
    if not sameroot(x, y):
        arr[root(y)] = root(x)


for i in range(m):
    merge(*tuple(map(ge, input().split())))

res = 0
for i in range(n):
    if sameroot(i, inp[i]):
        res += 1
print(res)

Buggy python submission (verdict: Time Limit Exceeded):
(n, m) = list(map(int, input().split()))
p = list(map(int, input().split()))
cG = []
cGT = []
for i in range(0, m):
    e = list(map(int, input().split()))
    aru = False
    do = []
    for c in cG:
        if e[0] in c or e[1] in c:
            if not do == []:
                mix = do + c
                cG.remove(do)
                cG.remove(c)
                cG.append(mix)
                break
            else:
                do = c
        if e[0] in c:
            aru = True
            c.append(e[1])
            cGT.append(e[1])
        elif e[1] in c:
            aru = True
            c.append(e[0])
            cGT.append(e[0])
    if not aru:
        cG.append(e)
        cGT = cGT + e

cant = list(set(p) - set(cGT))
res = 0
for i in cant:
    if p[i - 1] == i:
        res = res + 1

for i in range(1, n + 1):
    for c in cG:
        if i in c:
            ku = p.index(i) + 1
            if ku in c:
                res = res + 1
print(res)


Fixed submission:
def ge(x):
    return int(x) - 1


(n, m) = tuple(map(int, input().split()))
inp = list(map(ge, input().split()))
arr = [i for i in range(n)]


def root(a):
    global arr
    if arr[a] == a:
        return a
    else:
        tmp = root(arr[a])
        arr[a] = tmp
        return tmp


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


def merge(x, y):
    global arr
    if not sameroot(x, y):
        arr[root(y)] = root(x)


for i in range(m):
    merge(*tuple(map(ge, input().split())))

res = 0
for i in range(n):
    if sameroot(i, inp[i]):
        res += 1
print(res)

Buggy python submission (verdict: Time Limit Exceeded):
(n, m) = list(map(int, input().split()))
p = list(map(int, input().split()))
cG = []
for i in range(0, m):
    e = list(map(int, input().split()))
    aru = False
    do = []
    for c in cG:
        if e[0] in c or e[1] in c:
            if not do == []:
                mix = do + c
                cG.remove(do)
                cG.remove(c)
                cG.append(mix)
                break
            else:
                do = c
        if e[0] in c:
            aru = True
            c.append(e[1])
        if e[1] in c:
            aru = True
            c.append(e[0])
    if not aru:
        cG.append(e)

res = 0
for i in range(1, n + 1):
    cc = False
    for c in cG:
        if i in c:
            cc = True
            break
    if not cc:
        if p[i - 1] == i:
            res = res + 1

for i in range(1, n + 1):
    for c in cG:
        if i in c:
            ku = p.index(i) + 1
            if ku in c:
                res = res + 1
print(res)


Fixed submission:
def ge(x):
    return int(x) - 1


(n, m) = tuple(map(int, input().split()))
inp = list(map(ge, input().split()))
arr = [i for i in range(n)]


def root(a):
    global arr
    if arr[a] == a:
        return a
    else:
        tmp = root(arr[a])
        arr[a] = tmp
        return tmp


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


def merge(x, y):
    global arr
    if not sameroot(x, y):
        arr[root(y)] = root(x)


for i in range(m):
    merge(*tuple(map(ge, input().split())))

res = 0
for i in range(n):
    if sameroot(i, inp[i]):
        res += 1
print(res)

Buggy python submission (verdict: Time Limit Exceeded):
(n, m) = list(map(int, input().split()))
p = list(map(int, input().split()))
cG = []
for i in range(0, m):
    e = list(map(int, input().split()))
    aru = False
    do = []
    for c in cG:
        if e[0] in c or e[1] in c:
            if not do == []:
                mix = do + c
                cG.remove(do)
                cG.remove(c)
                cG.append(mix)
                break
            else:
                do = c
        if e[0] in c:
            aru = True
            c.append(e[1])
        if e[1] in c:
            aru = True
            c.append(e[0])
    if not aru:
        cG.append(e)

cant = []
for i in range(1, n + 1):
    cc = False
    for c in cG:
        if i in c:
            cc = True
            break
    if not cc:
        cant.append(i)

res = 0
for i in range(1, n + 1):
    for c in cG:
        if i in c:
            ku = p.index(i) + 1
            if ku in c:
                res = res + 1

for i in cant:
    if p[i - 1] == i:
        res = res + 1
print(res)


Fixed submission:
def ge(x):
    return int(x) - 1


(n, m) = tuple(map(int, input().split()))
inp = list(map(ge, input().split()))
arr = [i for i in range(n)]


def root(a):
    global arr
    if arr[a] == a:
        return a
    else:
        tmp = root(arr[a])
        arr[a] = tmp
        return tmp


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


def merge(x, y):
    global arr
    if not sameroot(x, y):
        arr[root(y)] = root(x)


for i in range(m):
    merge(*tuple(map(ge, input().split())))

res = 0
for i in range(n):
    if sameroot(i, inp[i]):
        res += 1
print(res)

Buggy python submission (verdict: Time Limit Exceeded):
(n, m) = list(map(int, input().split()))
p = list(map(int, input().split()))
pairs = []
for i in range(0, m):
    pairs.append(list(map(int, input().split())))
cG = []
for e in pairs:
    aru = False
    do = []
    for c in cG:
        if e[0] in c or e[1] in c:
            if not do == []:
                mix = do + c
                cG.remove(do)
                cG.remove(c)
                cG.append(mix)
                break
            else:
                do = c
        if e[0] in c:
            aru = True
            c.append(e[1])
        if e[1] in c:
            aru = True
            c.append(e[0])
    if not aru:
        cG.append(e)

cant = []
for i in range(1, n + 1):
    cc = False
    for c in cG:
        if i in c:
            cc = True
            break
    if not cc:
        cant.append(i)

res = 0
for i in range(1, n + 1):
    for c in cG:
        if i in c:
            ku = p.index(i) + 1
            if ku in c:
                res = res + 1
    if i in cant:
        if p[i - 1] == i:
            res = res + 1
print(res)


Fixed submission:
def ge(x):
    return int(x) - 1


(n, m) = tuple(map(int, input().split()))
inp = list(map(ge, input().split()))
arr = [i for i in range(n)]


def root(a):
    global arr
    if arr[a] == a:
        return a
    else:
        tmp = root(arr[a])
        arr[a] = tmp
        return tmp


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


def merge(x, y):
    global arr
    if not sameroot(x, y):
        arr[root(y)] = root(x)


for i in range(m):
    merge(*tuple(map(ge, input().split())))

res = 0
for i in range(n):
    if sameroot(i, inp[i]):
        res += 1
print(res)

Buggy python submission (verdict: Time Limit Exceeded):
(n, m) = list(map(int, input().split()))
p = list(map(int, input().split()))
pairs = []
for i in range(0, m):
    pairs.append(list(map(int, input().split())))
cG = []
for e in pairs:
    aru = False
    do = []
    for c in cG:
        if e[0] in c or e[1] in c:
            if not do == []:
                mix = do + c
                cG.remove(do)
                cG.remove(c)
                cG.append(mix)
                break
            else:
                do = c
        if e[0] in c:
            aru = True
            c.append(e[1])
        if e[1] in c:
            aru = True
            c.append(e[0])
    if not aru:
        cG.append(e)

cant = []
for i in range(1, n + 1):
    cc = False
    for c in cG:
        if i in c:
            cc = True
            break
    if not cc:
        cant.append(i)

res = 0
for c in cG:
    for i in range(1, n + 1):
        if i in c:
            ku = p.index(i) + 1
            if ku in c:
                res = res + 1

for i in cant:
    if p[i - 1] == i:
        res = res + 1
print(res)


Fixed submission:
def ge(x):
    return int(x) - 1


(n, m) = tuple(map(int, input().split()))
inp = list(map(ge, input().split()))
arr = [i for i in range(n)]


def root(a):
    global arr
    if arr[a] == a:
        return a
    else:
        tmp = root(arr[a])
        arr[a] = tmp
        return tmp


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


def merge(x, y):
    global arr
    if not sameroot(x, y):
        arr[root(y)] = root(x)


for i in range(m):
    merge(*tuple(map(ge, input().split())))

res = 0
for i in range(n):
    if sameroot(i, inp[i]):
        res += 1
print(res)