Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

X, K, D = map(int, input().split())
X=abs(X)
if D>=X and K%2==0:
    ans=X
elif D>=X and K%2==1:
    ans=D-X
elif D<X and K*D<X:
    ans=X-K*D
elif X<K*D-X:
    ans=X
    for i in range(0,K):
        ans=ans-D
        if ans<0 and (K-i)%2==0:
            ans=ans+D
            break
        elif ans<0 and (K-i)%2==1:
            ans=-ans
            break
else:
    ans=K*D-X
    for i in range(0,K):
        ans=ans-D
        if ans<0 and (K-i)%2==0:
            ans=ans+D
            break
        elif ans<0 and (K-i)%2==1:
            ans=-ans
            break
print(ans)

### FIXED CODE ###

X, K, D = map(int, input().split())
X=abs(X)
if D>=X and K%2==0:
    ans=X
elif D>=X and K%2==1:
    ans=D-X
elif D<X and K*D<X:
    ans=X-K*D
else:
    ans=X%D
    if (X-ans)%2!=K%2:
        ans=D-ans
print(ans)
### BUGGY CODE ###

N, M, Q = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(M)]
PQ = [list(map(int, input().split())) for _ in range(Q)]

lines = [[0] * N for _ in range(N)]

for l, r in LR:
	l, r = l - 1, r - 1
	lines[l][r] += 1

for p, q in PQ:
	p, q = p - 1, q - 1
	lines_sum = 0
	for y in range(p, q + 1):
		for x in range(p, q + 1):
			lines_sum += lines[y][x]
	print(lines_sum)


### FIXED CODE ###

N, M, Q = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(M)]
PQ = [list(map(int, input().split())) for _ in range(Q)]

table = [[0 for _ in range(N + 2)] for _ in range(N + 2)]

for l, r in LR:
	table[l][r] += 1

#for line in table:
#	print(*line)
#print()

for i in range(1, N + 1):
	for j in range(1, N + 1):
		table[i][j] += table[i][j - 1]
for i in range(1, N + 1):
	for j in range(1, N + 1):
		table[i][j] += table[i - 1][j]

#for line in table:
#	print(*line)
#print()

for p, q in PQ:
	#print(table[q][q], table[p - 1][q], table[q][p - 1], table[p - 1][p - 1])
	print(table[q][q] - table[p - 1][q] - table[q][p - 1] + table[p - 1][p - 1])
### BUGGY CODE ###

A, B, C, D, E, F = map(int, input().split())
con = 0
sugar_water = 0
sugar = 0
for a in range(30):
    for b in range(31):
        if not a == b == 0 and a*A*100 + b*B*100 <= F:
            for c in range(int((F - a*A*100 + b*B*100) / C) + 1):
                for d in range(int((F - a*A*100 + b*B*100 + c*C) / D) + 1):
                    if 100*(A*a+B*b)+C*c+D*d <= F:
                        if (c*C+d*D)/(100*a*A+100*b*B+c*C+d*D) <= E/(100+E):
                            if con <= 100*((c*C+d*D)/(100*a*A+100*b*B+c*C+d*D)):
                                con = 100*((c*C+d*D)/(100*a*A+100*b*B+c*C+d*D))
                                sugar = c*C+d*D
                                sugar_water = 100*a*A+100*b*B+c*C+d*D
print(sugar_water, sugar)

### FIXED CODE ###

A, B, C, D, E, F = map(int, input().split())
con = 0
sugar_water = 0
sugar = 0
for a in range(int(F / 100 / A) + 1):
    for b in range(int((F - a * 100 * A)/ 100 / B) + 1):
        if not a == b == 0:
            for c in range(int((a*A+b*B)*E/C)+1):
                for d in range(int(((a*A+b*B)*E-c*C)/D)+1):
                    if 100*(A*a+B*b)+C*c+D*d <= F:
                        if con <= 100*((c*C+d*D)/(100*a*A+100*b*B+c*C+d*D)):
                            con = 100*((c*C+d*D)/(100*a*A+100*b*B+c*C+d*D))
                            sugar = c*C+d*D
                            sugar_water = 100*a*A+100*b*B+c*C+d*D
print(sugar_water, sugar)
### BUGGY CODE ###

N = int(input())
A = list(map(int, input().split()))
t = A[0]
for i in range(1, len(A)):
    t = t * A[i]
if t > 1e18:
    print(-1)
else:
    print(t)


### FIXED CODE ###

#import numpy as np
N = int(input())
A = list(map(int, input().split()))
#A = np.array(A)
t = A[0]
for i in range(1,N):
    t *= A[i]
    if int(t) > 1e18:
        for j in range(i,N):
            if A[j] == 0:
                t = 0
        break
if int(t) <= 1e18:
    print(int(t))
else:
    print(-1)
### BUGGY CODE ###

total = 0
N,K = list(map(int,input().split()))
number_list = list(map(int,input().split()))
a = [sum(number_list[i:i+K]) for i in range(N-K+1)]
max_pattern = a.index(max(a))
print(sum(r * (1+r) / (2 * r) for r in number_list[max_pattern:max_pattern+K]))

### FIXED CODE ###

N,M=list(map(int,input().split()))
l_n=list(map(int,input().split()))
k=[]
j=0
for i in range(1,1001):
   j+=i
   k.append(j/i)
l_n=[k[l_n[i]-1] for i in range(N)]
ans=sum(l_n[0:M])
sum_n=ans
for i in range(N-M):
   sum_n+=l_n[i+M]
   sum_n-=l_n[i]
   ans=max(ans,sum_n)
print(ans)
### BUGGY CODE ###

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

for i in range(M//N+1,0,-1):
    if (M-(N-1)*i)%i==0 and M-(N-1)*i>0:
        ret = i
        print(ret)
        break

### FIXED CODE ###

def solve():
    n,m = map(int,input().split())
    for i in range(m//n,0,-1):
        if m%i==0: break
    print(i)
solve()
### BUGGY CODE ###

s = int(input())
progression = []
n = s
count = 1
while n not in progression:
    count += 1
    if n % 2 == 0:
        n = n/2
    else:
        n = 3 * n + 1
        
print(count)

### FIXED CODE ###

s = int(input())
progression = [s]
n = s
count = 1
while True:
    count += 1
    if n % 2 == 0:
        n = n/2
    else:
        n = 3 * n + 1

    if n in progression:
        break
    else:
        progression.append(n)

print(count)
### BUGGY CODE ###

import itertools

N = int(input())
A = list(map(int, input().split()))
# L = [(i, i + v, v - i) for i, v in enumerate(A)]
Ai = dict()
Aj = dict()
for i, v in enumerate(A):
    ans = 0
    ai = i + v
    aj = i - v
    if ai in Ai:
        Ai[ai].append(i)
    else:
        Ai[ai] = [i]
    if aj in Aj:
        Aj[aj].append(i)
    else:
        Aj[aj] = [i]
ans = 0
intersection_keys = Ai.keys() & Aj.keys()
for key in intersection_keys:
    L = itertools.product(Ai[key], Aj[key])
    ans += len([True for i, j in L if j > i])

print(ans)

### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))

Ai = dict()
ans = 0
for i, v in enumerate(A):
    ai = i + v
    if ai in Ai:
        Ai[ai].append(i)
    else:
        Ai[ai] = [i]
for i, v in enumerate(A):
    aj = i - v
    if aj in Ai:
        ans += len(Ai[aj])
print(ans)
### BUGGY CODE ###

import copy
N=int(input())
A = [int(input()) for i in range(N)]
for i in range(N):
    hoge=copy.copy(A)
    hoge.pop(i)
    print(max(hoge))

### FIXED CODE ###

N=int(input())
A = [int(input()) for i in range(N)]
B=sorted(A)
big=max(A)
flag=0
big2=B[-2]
for i in A:
    if i==big:
        print(big2)
    else:
        print(big)
### BUGGY CODE ###

N, K = map(int, input().split())
A = list(map(int, input().split()))

n = 1
ls = [1]
lps = 0
lpn = 0
for i in range(K):
    n = A[n-1]
    ls += [n]
    if A[n-1] in ls:
        lps = ls.index(A[n-1])
        lpn = i+2-lps
        break
if lpn != 0:
    print(ls[lps:][(K-lps)%lpn])
else:
    print(ls[lps:][K])

### FIXED CODE ###

N, K = map(int, input().split())
A = list(map(lambda s: int(s)-1, input().split()))

n = 0
visit = {0}
ls = [0]
for _ in range(K):
    n = A[n]
    if n not in visit:
        visit.add(n)
        ls += [n]
    else:
        loop_start = 0
        loop_length = 0
        flag = False
        m = 0
        for _ in range(N):
            if n == m:
                if flag:
                    break
                else:
                    flag = True
            if flag:
                loop_length += 1
            else:
                loop_start += 1
            m = A[m]

        print(ls[loop_start + ((K-loop_start) % loop_length)]+1)
        break
else:
    print(n+1)
### BUGGY CODE ###

from collections import defaultdict
import sys
input = sys.stdin.buffer.readline
def LI(): return list(map(int, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IR(n):
    res = [None] * n
    for i in range(n):
        res[i] = II()
    return res
def LIR(n):
    res = [None] * n
    for i in range(n):
        res[i] = LI_()
    return res
#solve
def solve():
    n, q = LI()
    c = LI()
    lr = LIR(q)
    for i in range(q):
        l, r = lr[i]
        lr[i] = [l,r,i]
    ans = [None] * q
    lr.sort(key=lambda x: x[1])
    dp = defaultdict(lambda:-1)
    bit = [0] * (n + 1)
    def add(i, w):
        """
        i番目にwを加える
        :param int i:
        :param int w:
        :return:
        """
        x = i + 1
        while x <= n:
            bit[x - 1] += w
            x += x & -x
        return
    def sum(i):
        """
        [0,i]の合計
        :param int i:
        :return:
        """
        res = 0
        x = i + 1
        while x > 0:
            res += bit[x - 1]
            x -= x & -x
        return res
    t = 0
    for i in range(n):
        ci = c[i]
        l = dp[ci]
        add(i, 1)
        dp[ci] = i
        if l != -1:
            add(l, -1)
        while t < q and lr[t][1] <= i:
            l, r, j = lr[t]
            tmp = sum(r) - sum(l - 1)
            ans[j] = tmp
            t += 1
    for ai in ans:
        print(ai) 
    return


#main
if __name__ == '__main__':
    solve()


### FIXED CODE ###

#!/usr/bin/env python3
from collections import defaultdict
import sys
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
def LI(): return list(map(int, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def LIR(n):
    res = [None] * n
    for i in range(n):
        l, r = map(int, input().split())
        res[i] = (l-1, r-1, i)
    return res

#solve
def solve():
    n, q = LI()
    c = LI()
    lr = LIR(q)
    ans = [None] * q
    lr.sort(key=lambda x: x[1])
    dp = [-1] * (max(c) + 1)
    bit = [0] * (n + 1)
    def add(i, w):
        """
        i番目にwを加える
        :param int i:
        :param int w:
        :return:
        """
        x = i + 1
        while x <= n:
            bit[x - 1] += w
            x += x & -x
        return
    def sum(i):
        """
        [0,i]の合計
        :param int i:
        :return:
        """
        res = 0
        x = i + 1
        while x > 0:
            res += bit[x - 1]
            x -= x & -x
        return res
    t = 0
    def f(t):
        l, r, j = lr[t]
        xr = sum(r)
        xl = sum(l-1)
        tmp = xr - xl
        ans[j] = tmp
        
    for i in range(n):
        ci = c[i]
        l = dp[ci]
        add(i, 1)
        dp[ci] = i
        if l != -1:
            add(l, -1)
        while t < q and lr[t][1]<= i:
            f(t)
            t += 1
        if t == q:
            break
    for ai in ans:
        print(ai) 
    return


#main
if __name__ == '__main__':
    solve()
### BUGGY CODE ###

#!/usr/bin/env python3

def main():
    n, m = map(int, input().split())
    ab = [list(map(int, input().split())) for i in range(m)]
    ans = "IMPOSSIBLE"
    for i in range(m):
        if ab[i][0] == 1:
            if [ab[i][1], n] in ab:
                ans = "POSSIBLE"
    print(ans)


if __name__ == "__main__":
    main()


### FIXED CODE ###

#!/usr/bin/env python3

def main():
    n, m = map(int, input().split())
    ab = [list(map(int, input().split())) for i in range(m)]
    ans = "IMPOSSIBLE"
    list_1 = []
    list_n = []
    for i in range(m):
        if ab[i][0] == 1:
            list_1.append(ab[i][1])
        elif ab[i][1] == n:
            list_n.append(ab[i][0])
    if len(set(list_1) & set(list_n)) > 0:
        ans = "POSSIBLE"
    print(ans)


if __name__ == "__main__":
    main()
### BUGGY CODE ###

import numpy
import bisect
N,Q = (int(x) for x in input().split())

stop_arr = []
for _ in range(N):
    S,T,X = (int(x) for x in input().split())
    stop_arr.append((S,T,X))
Q_arr = []
for _ in range(Q):
    Q_arr.append(int(input()))

stop_arr.sort(key=lambda x:x[2], reverse=True)

Q_arr = numpy.array(Q_arr)
ans_arr = numpy.array([-1]*Q)

for s,t,x in stop_arr:
    q_start = numpy.searchsorted(Q_arr, s-x)
    q_stop = numpy.searchsorted(Q_arr, t-x)
    ans_arr[q_start:q_stop].fill(x)

for a in ans_arr:
    print(a)


### FIXED CODE ###

import bisect

N,Q = (int(x) for x in input().split())

stop_arr = [tuple(map(int, input().split())) for _ in range(N)]
Q_arr = [int(input()) for _ in range(Q)]

stop_arr_ind = list(range(N))
stop_arr_ind.sort(key=lambda x:stop_arr[x][2])
# stop_arr.sort(key=lambda x:x[2], reverse=True)
ans_arr = [-1]*Q
skip_arr = [-1]*(Q+1)

def update_skip(skip_arr, x):
    if skip_arr[x] == -1:
        return x
    x_dest = update_skip(skip_arr, skip_arr[x])
    skip_arr[x] = x_dest
    return x_dest

for i in stop_arr_ind:
    s,t,x = stop_arr[i]
    q_start = bisect.bisect_left(Q_arr, s-x)
    q_stop = bisect.bisect_left(Q_arr, t-x)
    l = q_start
    while l < q_stop:
        if skip_arr[l] == -1:
            skip_arr[l] = q_stop
            ans_arr[l] = x
            l += 1
        else:
            l = update_skip(skip_arr, l)

print(*ans_arr, sep="\n")
### BUGGY CODE ###

a=int(input())
b=input().split(' ')
m=0
s=1
i=0
o=0
while i<a:
    if int(b[i])==0:
        print(0)
        o=o+1
    i=i+1
while m<a:
    s=s*int(b[m])
    m=m+1
    if s>10**18 and o==0:
        print(-1)
        o=o+1
        break

if o==0:
    print(s)


### FIXED CODE ###

a=int(input())
b=input().split(' ')
m=0
s=1
i=0
o=0
while i<a:
    if int(b[i])==0:
        print(0)
        o=o+1
    i=i+1
while m<a and o==0:
    s=s*int(b[m])
    m=m+1
    if s>10**18 and o==0:
        print(-1)
        o=o+1
        break

if o==0:
    print(s)
### BUGGY CODE ###

import numpy as np

n = int(input())
a = list(map(int, input().split()))
len_a = len(a)
range_a = range(len_a)
times1 = []
times2 = []

for i in range_a:
    times1 += [a[i]] * (len_a - (i + 1))
    times2 += a[i + 1 : ]

result = np.array(times1, dtype = "uint64") * np.array(times2, dtype = "uint64")

print(int(sum(result) % (10 ** 9 + 7)))


### FIXED CODE ###

n = input()
a = list(map(int, input().split()))
sum_a = sum(a)
count = 0
for i, v in enumerate(a):
    sum_a -= v
    count += v * sum_a

print(count % (10 ** 9 + 7))
### BUGGY CODE ###

n, k = list(map(int,input().split()))
a = list(map(int,input().split()))
# 循環を調べる
tmp_city=[]
c=0
count=0
init_city=0

while (1):
    count+=1
    tmp=a[c]
    if k==count:
        tmp_city.append(tmp)
        tmp_city=[1]+tmp_city
        break
    if tmp == 1:
        init_city=0
        tmp_city=[1]+tmp_city
        break
    if tmp in tmp_city:
        init_city=tmp_city.index(tmp)+1
        tmp_city=[1]+tmp_city
        break
    tmp_city.append(tmp)
    c=tmp-1
if k==count:
    print(tmp_city[-1])
else:
    print(tmp_city[init_city:][(k-(init_city))%len(tmp_city[init_city:])])

### FIXED CODE ###

n, k = map(int, input().split())
A = list(map(int, input().split()))
D = [0] * (n+1)
now = 1
for i in range(1, k+1):
    tmp = A[now-1]
    if not D[tmp]:
        now = tmp
        D[now] += i
    else:
        pre = D[tmp]
        loop = i - D[tmp]
        if loop>1:
            now=D.index((k - pre) % loop + pre)
        else:
            now=tmp
        break
print(now)
### BUGGY CODE ###

#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**8)
from functools import lru_cache
INF = float("inf")

MOD = 998244353  # type: int


def main():
    A, B, C, D = map(int, input().split())

    @lru_cache(maxsize=None)
    def f(a, b):
        if a < A or b < B:
            return 0
        if a == A and b == B:
            return 1
        elif a == A:
            return A**(b-B) % MOD
        elif b == B:
            return B**(a-A) % MOD
        ans = f(a-1, b)*b
        ans %= MOD
        ans += f(a, b-1)*a
        ans %= MOD
        ans -= f(a-1, b-1)*(a-1)*(b-1)
        ans %= MOD
        return ans

    print(f(C, D))

    return


if __name__ == '__main__':
    main()


### FIXED CODE ###

#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**8)
from functools import lru_cache
INF = float("inf")

MOD = 998244353  # type: int


def main():
    A, B, C, D = map(int, input().split())
    DP = [[1]*(D-B+1) for _ in range(C-A+1)]

    for i in range(C-A+1):
        for j in range(D-B+1):
            if i == j == 0:
                continue
            DP[i][j] = 0
            if 0 <= i-1:
                DP[i][j] += (DP[i-1][j]*(j+B)) % MOD
                DP[i][j] %= MOD
            if 0 <= j-1:
                DP[i][j] += (DP[i][j-1]*(i+A)) % MOD
                DP[i][j] %= MOD
            if 0 <= i-1 and 0 <= j-1:
                DP[i][j] -= (DP[i-1][j-1]*(i+A-1)*(j+B-1)) % MOD
                DP[i][j] %= MOD
            # print(i, j, DP[i][j])
    print(DP[-1][-1] % MOD)
    # print(DP)

    # @lru_cache(maxsize=None)
    # def f(a, b):
    #     if a < A or b < B:
    #         return 0
    #     if a == A and b == B:
    #         return 1
    #     elif a == A:
    #         return pow(A, b-B, MOD)
    #     elif b == B:
    #         return pow(B, a-A, MOD)
    #     ans = (f(a-1, b)*b) % MOD
    #     ans += (f(a, b-1)*a) % MOD
    #     ans -= (f(a-1, b-1)*(a-1)*(b-1)) % MOD
    #     ans %= MOD
    #     return ans

    # print(f(C, D))

    return


if __name__ == '__main__':
    main()
### BUGGY CODE ###

N = int(input())
A = [int(input()) for i in range(N)]

for i in range(N):
    left = [A[j] for j in range(0, i)]
    right = [A[j] for j in range(1+i, N)]
    print(max(right + left))

### FIXED CODE ###

N = int(input())
A = [int(input()) for i in range(N)]

FIRST = max(A)
if A.count(FIRST) > 1:
    for i in range(N):
        print(FIRST)
else:
    SECOND = max([a for a in A if a != FIRST])
    for a in A:
        if a == FIRST:
            print(SECOND)
        else:
            print(FIRST)
### BUGGY CODE ###

import math
# メモを残す
K = int(input()) + 1
dic = {}
ans = 0
for a in range(1,K):
    for b in range(1,K):
        for c in range(1,K):
            # 大きい順にソートする
            l = tuple(sorted([a,b,c]))
            if l in dic:
                ans += dic[l]
            else:
                # 最大公約数を求める
                tmp_l = tuple([l[0], l[1]])
                if tmp_l in l:
                    gcd_ab = l[tmp_l]
                else:
                    gcd_ab = math.gcd(l[0], l[1])
                    dic[tmp_l] = gcd_ab

                if gcd_ab < c:
                    tmp_l = tuple([gcd_ab, c])
                else:
                    tmp_l = tuple([c, gcd_ab])

                if tmp_l in l:
                    gcd = l[tmp_l]
                else:
                    gcd = math.gcd(gcd_ab, c)
                    dic[tmp_l] = gcd
                dic[l] = gcd
                ans+=gcd
print(ans)

### FIXED CODE ###

from math import gcd
k = int(input())
ans = 0
for a in range(1, k+1):
  for b in range(1, k+1):
    for c in range(1, k+1):
      ans += gcd(a, gcd(b, c))
print(ans)
### BUGGY CODE ###

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

ans = 1
flag = 0
for i in range(n):
    ans *= a[i]
    if ans > 10**18:
        flag = 1

if flag == 1 and ans != 0:
    ans = -1

print(ans)


### FIXED CODE ###

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

ans = 1
flag = 0
for i in range(n):
    ans *= a[i]
    if ans > 10**18:
        flag = 1
        break

if flag == 1 and (0 not in a):
    ans = -1
if 0 in a:
    ans = 0

print(ans)
### BUGGY CODE ###

N = int(input())
A = [int(a) for a in input().split()]

dp = [[0] * N for _ in range(N)]

for d in range(1, N):
    for l in range(N-d):
        r = l + d
        if d == 0:
            dp[l][r] = A[l]
            
        else:
            dp[l][r] = 10**20
            for m in range(l, r):
                dp[l][r] = min(dp[l][r], dp[l][m] + dp[m+1][r] + sum(A[l:r+1]))
    

print(dp[0][N-1])

### FIXED CODE ###

N = int(input())
A = [int(a) for a in input().split()]
B = [0]
for a in A:
    B.append(B[-1]+a)

dp = [[0] * N for _ in range(N)]

for d in range(1, N):
    for l in range(N-d):
        r = l + d
        if d == 0:
            dp[l][r] = A[l]
            
        else:
            dp[l][r] = 10**20
            for m in range(l, r):
                dp[l][r] = min(dp[l][r], dp[l][m] + dp[m+1][r] + B[r+1]-B[l])
    
print(dp[0][N-1])
### BUGGY CODE ###

from collections import defaultdict
class UnionFind:
    def __init__(self,nodes):
        self.parents = {node : node for node in nodes}
    def root(self,x):
        if x == self.parents[x]:
            return x
        else:
            rt = self.root(self.parents[x])
            self.parents[x] = rt
            return rt
    def find(self,x,y):
        return self.root(x) == self.root(y)
    def union(self,x,y):
        self.parents[self.root(x)] = self.root(y)
    def get_sets(self):
        res = defaultdict(list)
        for node, parent in self.parents.items():
            res[self.root(node)].append(node)
        # return [*res.values()]
        return list(res.values())

N,M = map(int,input().split(' '))
p = list(map(int,input().split(' ')))

uf = UnionFind(range(1,N+1))

for i in [0]*M:
    x, y = map(int,input().split(' '))
    uf.union(x,y)

sets = uf.get_sets()

ans = 0
for s in sets:
    for n in s:
        if p[n-1] in s:
            ans += 1
print(ans)





### FIXED CODE ###

from collections import defaultdict
class UnionFind:
    def __init__(self,nodes):
        self.parents = {node : node for node in nodes}
    def root(self,x):
        if x == self.parents[x]:
            return x
        else:
            rt = self.root(self.parents[x])
            self.parents[x] = rt
            return rt
    def find(self,x,y):
        return self.root(x) == self.root(y)
    def union(self,x,y):
        self.parents[self.root(x)] = self.root(y)
    def get_sets(self):
        res = defaultdict(list)
        for node, parent in self.parents.items():
            res[self.root(node)].append(node)
        # return [*res.values()]
        return list(res.values())

N,M = map(int,input().split(' '))
p = list(map(int,input().split(' ')))

uf = UnionFind(range(1,N+1))

for i in [0]*M:
    x, y = map(int,input().split(' '))
    uf.union(x,y)

ans = 0
for n in range(1,N+1):
    if uf.find(n,p[n-1]):
        ans += 1
print(ans)
### BUGGY CODE ###

n=int(input())
List=list(map(int,input().split()))
cnt=0
for i in range(len(List)):
    if List[i]<=min(List[:i+1]):
        cnt+=1
print(cnt)

### FIXED CODE ###

n=int(input())
p=list(map(int,input().split()))
cnt=0
min_num=p[0]
for i in p:
    if i<=min_num:
        min_num=i
        cnt+=1
print(cnt)
### BUGGY CODE ###

N=int(input())
numbers=list(map(int,input().strip().split()))

L=len(numbers)
target_number=1
i=0
while L>i+1:
  if(numbers[i]!=target_number):
    numbers.pop(target_number-1)
    L-=1
  elif(numbers[i]==target_number):
    i+=1
    target_number+=1

if(L>1):
  print(N-target_number)
  
elif(L==1):
  if(numbers[0]==1):
    print(N-1)
  elif(numbers[0]!=1):
    print(-1)

### FIXED CODE ###

N=int(input())
numbers=list(map(int,input().strip().split()))

target_number=1

for i in range(N):
  if(numbers[i]==target_number):
    target_number+=1
  
if(target_number>1):
  print(N-target_number+1)
  
elif(target_number==1):
  if(1 in numbers):
    print(N-1)
  else:
    print(-1)
### BUGGY CODE ###

N = int(input())
A = [int(x) for x in input().split()]
Q = int(input())
BC = [[int(x) for x in input().split()]for i in range(Q)]

for i in range(Q):
  b = BC[i][0]
  c = BC[i][1]
  A = [c if i == b else i for i in A]
  print(sum(A))



### FIXED CODE ###

import collections
N = int(input())
a = [int(x) for x in input().split()]
aa = collections.Counter(a)
A = list(aa.items())
Alisted = [0]*(10**5+1)
for i in range(len(A)):
  Alisted[A[i][0]] = A[i][1]
Q = int(input())
BC = [[int(x) for x in input().split()]for i in range(Q)]

Sum = sum(a)
for i in range(Q):
  b = BC[i][0]
  c = BC[i][1]
  num = Alisted[b]
  Sum += (c-b)*num
  print(Sum)
  Alisted[b]=0
  Alisted[c]+=num
### BUGGY CODE ###

s=str(input())

p1 = '01'
p2 = '10'
s = s[::-1]
c = 0
flag = True
while flag:
  flag = False
  for i in range(len(s)):
    if s[i:i+4] in ('0011','1100','1010','0101','1001','0110'):
      s = s[:i] + s[i+4:]
      c += 4
      flag = True
      break
    
    
    if s[i:i+2] in (p1,p2):
      s = s[:i] + s[i+2:]
      c += 2
      flag = True
      break
print(c)

### FIXED CODE ###

s=str(input())
c1 = 0
c0 = 0
for i in range(len(s)):
  if s[i] == '1':
    c1 += 1
  if s[i] == '0':
    c0 += 1
print(2*min(c1,c0))