Harryxun/stf_run huggingface dataset:
import math
x,y = map(int,input().split())

def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9+7 #出力の制限
N = 10**6
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )

if (x+y)%3!=0:
    print(0)
else:
    a = x-(x+y)//3
    b = y-(x+y)//3
    print(cmb(a+b,a,inf))

### FIXED CODE ###

x, y = map(int, input().split())
if (x+y)%3 != 0:
    print(0)
    exit()

def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod
 
mod = 10**9+7
N = 10**6
g1 = [1, 1]
g2 = [1, 1]
inverse = [0, 1]
 
for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )
 
 
a = (2*x-y)//3
b = (2*y-x)//3
n = a+b
r = a
res = cmb(n,r,mod)
print(res)
n, r = int(input().split())
if n < 10:
  print(100 * (10 - n) + r)
else:
  print(r)

### FIXED CODE ###

hako = list(map(int, input().split()))
if hako[0] < 10:
  print(100 * (10 - hako[0]) + hako[1])
else:
  print(hako[1])
num = sys.stdin.readlines()
print(int(num)**3)

### FIXED CODE ###

import sys
num = int(sys.stdin.readline())
print(num**3)
N=int(input())
d={}
for i in list(map(int,input().split())):
    d[i]=d.get(i,0)+1
d=list(d.values())
if len(d)<3:
    ans=sum(d)
else:    
    for j in range(len(d)-2):
        s=d[j]+d[j+1]+d[j+2]
        if s>ans:
            ans=s
print(ans)

### FIXED CODE ###

from collections import Counter
N=int(input())
s=[]
for i in list(map(int,input().split())):
    s+=[i-1,i,i+1]
s=Counter(s)
print(s.most_common(1)[0][1])
H,W,m=map(int,input().split())
bomb=[[0]*W for _ in range(H)]
cnt1=[[0,i] for i in range(H)]
cnt2=[[0,i] for i in range(W)]
for i in range(m):
    h,w=map(int,input().split())
    bomb[h-1][w-1]=1
    cnt1[h-1][0]+=1
    cnt2[w-1][0]+=1
cnt1.sort(reverse=True)
cnt2.sort(reverse=True)
m1=cnt1[0][0]
l1=[]
i=0
while cnt1[i][0]==m1:
    l1.append(cnt1[i][1])
    i+=1
    if i>=H-1:
        break
m2=cnt2[0][0]    
l2=[]
i=0
while cnt2[i][0]==m2:
    l2.append(cnt2[i][1])
    i+=1
    if i>=W-1:
        break
ans=0
for i in l1:
    for j in l2:
        ans=max(ans,m1+m2-bomb[i][j])
print(ans)

### FIXED CODE ###

def resolve():
    #n=int(input())
    #a,b=map(int,input().split())
    #x=list(map(int,input().split()))
    #a=[list(map(lambda x:int(x)%2,input().split())) for _ in range(h)]
    import sys
    input = sys.stdin.readline
    H,W,m=map(int,input().split())
    bomb=set()
    cnt1=[0 for i in range(H)]
    cnt2=[0 for i in range(W)]
    for i in range(m):
        h,w=map(int,input().split())
        bomb.add((h-1,w-1))
        cnt1[h-1]+=1
        cnt2[w-1]+=1
    m1=max(cnt1)
    m2=max(cnt2)
    hk,wk=[],[]
    for i,x in enumerate(cnt1):
        if x==m1:
           hk.append(i)
    for i,x in enumerate(cnt2):
        if x==m2:
           wk.append(i)
    ans=m1+m2
    for i in hk:
        for j in wk:
            if (i,j) in bomb:
                continue
            print(ans)
            exit()
    print(ans-1)

if __name__ == '__main__':
    resolve()
from itertools import product
 
def gcb(a, b):
  	if a < b:
      	a, b = b, a
    if b == 0:
        gcb_table[a][b] = a
    else:
        if gcb_table[a][b] == -1:
            gcb_table[a][b] = gcb(b, a%b)
    return gcb_table[a][b]
  
k = int(input())
gcb_table = [[-1 for _ in range(k+1)] for _ in range(k+1)]
 
for i in range(1, k+1):
    for j in range(1, k+1):
        temp = gcb(i, j)
        gcb_table[i][j] = temp
        gcb_table[j][i] = temp
        
res = 0
l_k = range(1, k+1)
for part in itertools.product(l_k, l_k, l_k):
    gcb_first = gcb_table[part[0]][part[1]]
    res += gcb_table[gcb_first][part[2]]
print(res)

### FIXED CODE ###

def gcb(a, b):
    if a < b:
        a, b = b, a
    if b == 0:
        gcb_table[a][b] = a
    else:
        if gcb_table[a][b] == -1:
            gcb_table[a][b] = gcb(b, a%b)
    return gcb_table[a][b]

k = int(input())
gcb_table = [[-1 for _ in range(k+1)] for _ in range(k+1)]

for i in range(1, k+1):
    for j in range(1, k+1):
        temp = gcb(i, j)
        gcb_table[i][j] = temp
        gcb_table[j][i] = temp
        
res = 0
for a in range(1, k+1):
    for b in range(1, k+1):
        gcb_first = gcb_table[a][b]
        for c in range(1, k+1):
            res += gcb_table[gcb_first][c]
print(res)
k, x = int(input()), int(input())
print ("yes") if k*500 >= x else ("no")

### FIXED CODE ###

k, x = input().split()
if int(k)*500 >= int(x):
	print("Yes")
else:
	print("No")
import collections
N = int(input())
S = [input() for i in range(N)]
s = collections.Counter(S) #出現回数をまとめた辞書型作成

maximum = max(S.values())
list = sorted(key for key,value in s.items() if value==maximum))
#sを利用して、出現回数が最大のものを辞書順に並べたリストを作成

for i in list:
    print(i)

### FIXED CODE ###

#bA
import collections
N = int(input())
S = [input() for i in range(N)]
s = collections.Counter(S) #出現回数をまとめた辞書型作成

maximum = max(s.values()) #
list = sorted(key for key,value in s.items() if value==maximum)
#sを利用して、出現回数が最大のものを辞書順に並べたリストを作成

for i in list:
    print(i)
s=input()
length=len(s)-2
print(f'{s[0]}{length}{s[-1]}')

### FIXED CODE ###

s=input()
length=len(s)-2
print(s[0]+str(length)+s[-1])
import math


# def get_pseudo_input():
#     N = 3
#     A = [3, 4, 5]
#     return N, A


def get_input():
    N = int(input())
    A = [int(x) for x in input().split()]
    return N, A


def sieve_of_eratosthenes(x: int):
    ret = [-1] * (x + 1)
    for i in range(1, x + 1):
        ret[i] = i
    p_max = int(math.sqrt(x))
    for p in range(2, p_max + 1):
        if ret[p] < p:  # p is not a prime number
            continue
        for i in range(p, x + 1, p):
            if ret[i] > p:
                ret[i] = p
    return ret


A_max = 100000
p_table = sieve_of_eratosthenes(A_max)

N, A = get_input()
# Store a number of unique Ais using the prime number
p_nums = [0] * (A_max + 1)
max_p_nums = 0
for a in A:
    # print(f"a={a}")
    ps = set()
    while a > 1:
        p = p_table[a]
        a = int(a / p)
        # print(f"  a={a}")
        # Count up at most 1 for one a
        if p not in ps:
            p_nums[p] += 1
            ps.add(p)
        if max_p_nums < p_nums[p]:
            max_p_nums = p_nums[p]
# print(max_p_nums)
if max_p_nums == 1:
    print('pairwise coprime')
elif max_p_nums < N:
    print('setwise coprime')
else:
    print('not coprime')

### FIXED CODE ###

import math


def get_pseudo_input():
    N = 3
    A = [6, 10, 16]
    return N, A


def get_input():
    N = int(input())
    A = [int(x) for x in input().split()]
    return N, A


def sieve_of_eratosthenes(x: int):
    ret = [-1] * (x + 1)
    for i in range(1, x + 1):
        ret[i] = i
    p_max = int(math.sqrt(x))
    for p in range(2, p_max + 1):
        if ret[p] < p:  # p is not a prime number
            continue
        for i in range(p, x + 1, p):
            if ret[i] > p:
                ret[i] = p
    return ret


A_max = 10 ** 6
p_table = sieve_of_eratosthenes(A_max)

N, A = get_input()
# Store a number of unique Ais using the prime number
p_nums = [0] * (A_max + 1)
p_nums[1] = N
max_p_nums = 0
for a in A:
    # print(f"a={a}")
    while a > 1:
        p = p_table[a]
        while a % p == 0:
            a = a // p
        # print(f"  a={a}")
        p_nums[p] += 1
        if max_p_nums < p_nums[p]:
            max_p_nums = p_nums[p]
# print(max_p_nums)
if max_p_nums <= 1:
    # GCD of A is 1
    print('pairwise coprime')
elif max_p_nums == N:
    # GCD of A is not 1
    print('not coprime')
else:
    print('setwise coprime')
import numpy as np
n,m=map(int,input().split())
a,b=[],[]
for i in range(n):
    a.append(list(map(int,input().split())))
for i in range(m):
    b.append(list(map(int,input().split())))
a=np.array(a)
b=np.array(b)
x=np.dot(a, b, out = None)
for i in x:
    print(*i)

### FIXED CODE ###

n,m=map(int,input().split())
a,b=[],[]
for i in range(n):
    a.append(list(map(int,input().split())))
for i in range(m):
    b.append(int(input()))
for i in range(n):
    x=0
    for j in range(m):
        x+=a[i][j]*b[j]
    print(x)
inp = list(map(int,input().split()))
l = inp.sort()
print(l[0],l[1],l[2])

### FIXED CODE ###

inp = list(map(int,input().split()))
inp.sort()
print(inp[0],inp[1],inp[2])
n = int(input())
a = int(input())
b = int(input())


return (n * b + (a - b)) - (n * a + (b - a)) + 1

### FIXED CODE ###

n,a,b = map(int, input().split())
mi = a*(n-1)+b
ma = b*(n-1)+a
if ma -mi+1 < 0:
    print(0)
    quit()
print(ma-mi+1)
n = int(input())
a = list(map(int,input().split()))

s = []
p = 0

for i in range(n):
    p+=a[i]
    s.append(p)
s.append(0)

x = 0
import collections
s = collections.Counter(s)


for i in range(len(s)):
    x += (s[i][1]*(z[i][1]-1))//2
print(x)


### FIXED CODE ###

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

s = []
p = 0

for i in range(n):
    p+=a[i]
    s.append(p)
s.append(0)

x = 0
import collections
s = collections.Counter(s)

y,z = zip(*s.most_common())

for i in range(len(z)):
    x += (z[i]*(z[i]-1))//2
print(x)
import heapq
import sys
input = sys.stdin.readine
n, m = map(int, input().split(" "))
liste = list(map(int, input().split(" ")))
liste = [(-1 * i) for i in liste]
heapq.heapify(liste)
for i in range(m):
    maxi = heapq.heappop(liste)
    maxi /= 2
    heapq.heappush(liste, int(maxi))
liste = [(-1 * i) for i in liste]
print(sum(liste))

### FIXED CODE ###

import heapq
n, m = map(int, input().split(" "))
liste = list(map(int, input().split(" ")))
liste = [(-1 * i) for i in liste]
heapq.heapify(liste)
for i in range(m):
    maxi = heapq.heappop(liste)
    maxi /= 2
    heapq.heappush(liste, int(maxi))
sum = 0
for i in liste:
    sum += (-1 * i)
print(sum)
n, k = map(int, input().split())
d = map(str, input().split())
digits = set(d)
num = {str(i) for i in range(0,10)}
left = num - digits
for i in range(N, 100000):
    Num = set(str(i))
    if Num <= left:
        print(i)
        break

### FIXED CODE ###

s, k = [int(i) for i in input().split(" ")]
num = [int(i) for i in input().split(" ")]

def check(n):
	for _s in str(n):
		if int(_s) in num:
			return True
	return False

while check(s):
	s += 1
	
print(s)
n = int(input())
str = input()
char_list = list(str)
#del_num = []

count =0
ans =0

#dellist = lambda items, indexes: [item for index, item in enumerate(items) if index not in indexes]
for i in range(len(char_list)-1):
    if char_list[i] == char_list[i+1]:
        #del_num.append(i+1)
        count += 1

ans = n - count

print(ans)


### FIXED CODE ###

a,b = map(int,input().split())

if a-2*b>0:
    print(a-2*b)
else:
    print(0)
import bisect

N, D, A = map(int, input().split())
XH = [list(map(int, input().split())) for _ in range(N)]


class BIT:
    def __init__(self, n=None):
        # 1-index
        if n is None:
            n = (1 << 20) - 1
        self.n = n
        self.dat_a = [0] * (2 * self.n - 1)
        self.dat_b = [0] * (2 * self.n - 1)

    def add(self, a, b, x, k=0, l=0, r=None):
        # Add x to [a, b)
        if r is None:
            r = self.n

        if a <= l and r <= b:
            self.dat_a[k] += x
        elif l < b and a < r:
            self.dat_b[k] += (min(b, r) - max(a, l)) * x
            self.add(a, b, x, k * 2 + 1, l, (l + r) // 2)
            self.add(a, b, x, k * 2 + 2, (l + r) // 2, r)
            
    def query(self, a, b, k=0, l=0, r=None):
        # Retrun sum of [a, b)
        if r is None:
            r = self.n
        
        if b <= l or r <= a:
            return 0
        
        if a <= l and r <= b:
            return self.dat_a[k] * (r - l) + self.dat_b[k]
        
        res = (min(b, r) - max(a, l)) * self.dat_a[k]
        res += self.query(a, b, k * 2 + 1, l, (l + r) // 2)
        res += self.query(a, b, k * 2 + 2, (l + r) // 2, r)
        
        return res

X, H = zip(*sorted(XH))
bit = BIT(N + 10)

# 初期化
for i, h in enumerate(H):
    bit.add(i + 1, i + 2, h)
    
# 処理
res = 0
for i in range(N):
    cur = bit.query(i + 1, i + 2)
    if cur <= 0:
        continue

    need = -(-cur // A)
    idx = bisect.bisect_right(X, X[i] + 2 * D)
    bit.add(i + 1, idx + 1, -need * A)
    res += need
    
print(res)


### FIXED CODE ###

import bisect

N, D, A = map(int, input().split())
XH = [list(map(int, input().split())) for _ in range(N)]

XH.sort()
X, H = zip(*XH)

K = [-(-h // A) for h in H]
L = [0] * (N + 2)

ans = 0
for i in range(N):
    L[i + 1] += L[i]
    s = max(K[i] - L[i + 1], 0)
    ans += s
    L[i + 1] += s
    L[bisect.bisect_right(X, X[i] + 2 * D) + 1] -= s

print(ans)
s = input()
t = input()
flag = False
for i in range(len(s)):
    if s == t:
        flag =True
    s = s[-1] + s[::len(s)-1
if flag:
    print("Yes")
else:
    print("No")


### FIXED CODE ###

s = input()
t = input()
flag = False
for i in range(len(s)):
    if s == t:
        flag =True
    s = s[-1] + s[0:len(s)-1]
if flag:
    print("Yes")
else:
    print("No")
n,m=[int(i)for i in input().split()]
list=[int(i) for i in input().split()]
x=1/(4*m)
count=0
for i in range(1,n+1):
  if list[i]>=x:
    count+=1

if count>=m:
  print('Yes')
else:
  print('No'

### FIXED CODE ###

n,m=[int(i) for i in input().split()]
list=[int(i) for i in input().split()]
x=1/(4*m)
y=sum(list)
count=0
for i in range(1,n+1):
  if list[i-1]>=x*y:
    count+=1

if count>=m:
  print('Yes')
else:
  print('No')
N = int(input())
res = False
for i in range(N):
  if i * i > N:
    break
  else:
    q = N // i
    if i * q == N and 1 <= i <= 9 and 1 <= q <= 9:
      res = True
      break
print('Yes' if res else 'No')

### FIXED CODE ###

N = int(input())
res = False
for i in range(1, N+1):
  if i * i > N:
    break
  else:
    q = N // i
    if i * q == N and 1 <= i <= 9 and 1 <= q <= 9:
      res = True
      break
print('Yes' if res else 'No')
while True:
    x = input()
    if int(x) > 100 or int(x) < 1:
        continue
    print(int(x)**3)

### FIXED CODE ###

x = input()
y = int(x)**3
print(y)
a = [int(input()) for i in range(5)]
r = 10
last = 10000
for i in range(5):
    #if a[i] % 10 == 0:
    #    fix = i
    if a[i] % 10 != 0 and a[i] % 10 < r:
        last = i
        r = int(a[i] % 10)

if last == 10000:
    for i in range(5):
        time = time + a[i]

else:
    b = [int((a[i] + 9) / 10) * 10 for i in range(5)] 
    time = 0
    for i in range(5):
        if i != last:
            time = time + b[i]
    
    time = time + a[last]
    print(time)

### FIXED CODE ###

a = [int(input()) for i in range(5)]
r = 10
last = 0
for i in range(5):
    if a[i] % 10 != 0 and a[i] % 10 < r:
        last = i
        r = int(a[i] % 10)


b = [int((a[i] + 9) / 10) * 10 for i in range(5)] 
time = 0
for i in range(5):
    if i != last:
        time = time + b[i]
    
time = time + a[last]
print(time)
a,b=map(int,input().split())
s=input()
for i in range(a):
    if s[i]=='-':
        print('No')
        sys.exit()
if  s[a]!='-':
    print('No')
    sys.exit()
for i in range(b):
    if s[i+a+1]=='-':
        print('No')
        sys.exit()
print('Yes')

### FIXED CODE ###

import sys
a,b=map(int,input().split())
s=input()
for i in range(a):
    if s[i]=='-':
        print('No')
        sys.exit()
if  s[a]!='-':
    print('No')
    sys.exit()
for i in range(b):
    if s[i+a+1]=='-':
        print('No')
        sys.exit()
print('Yes')
X,A,B=map(int,input().split())

if A>B:
  print('delicious')
else:
  if X>=B-A:
    print('safe')
  else:
    pint('dangerous')

### FIXED CODE ###

X,A,B=map(int,input().split())
 
if A>=B:
  print('delicious')
else:
  if X>=B-A:
    print('safe')
  else:
    print('dangerous')
import math
import sys

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

if N == 2:
    print(max(A))
    sys.exit(0)

L = [0 for i in range(N)]
R = [0 for i in range(N)]
L[0] = A[0]
R[-1] = A[-1]

for i in range(1, N):
    L[i] = math.gcd(L[i-1], A[i])
    R[-i-1] = math.gcd(R[-i], A[-i-1])

# print(L)
# print(R)

max_gcd = max(L[-2], R[1])
for i in range(1, N-1):
    print(math.gcd(L[i-1], R[i+1]))
    max_gcd = max(max_gcd, math.gcd(L[i-1], R[i+1]))

print(max_gcd)
#min_g = max(G1[-1], G2[1])
#min_g2 = [math.gcd(G1[i], G2[i+1]) for i in range(N-2)]


### FIXED CODE ###

import sys
from fractions import gcd

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

if N == 2:
    print(max(A))
    sys.exit(0)

L = [0 for i in range(N)]
R = [0 for i in range(N)]
L[0] = A[0]
R[-1] = A[-1]

for i in range(1, N):
    L[i] = gcd(L[i-1], A[i])
    R[-i-1] = gcd(R[-i], A[-i-1])

# print(L)
# print(R)

max_gcd = max(L[-2], R[1])
for i in range(1, N-1):
    #print(math.gcd(L[i-1], R[i+1]))
    max_gcd = max(max_gcd, gcd(L[i-1], R[i+1]))

print(max_gcd)
#min_g = max(G1[-1], G2[1])
#min_g2 = [math.gcd(G1[i], G2[i+1]) for i in range(N-2)]
a, b, c = input_raw().split()
if a<b<c:
    print "Yes"
else:
    print "No"

### FIXED CODE ###

a,b,c = raw_input().split()
if a<b<c:
    print 'Yes'
else:
    print 'No'
s = list(input())
k = int(input())

if s[0] == '1' and len(s) == 1:
  print(s[0])
elif s[0] == '1' and s[:k] == ['1']*k and len(s) != 1:
  print(s[1])
elif s[0] == '1' and s[1] != '1':
  print(s[1])
elif s[0]!='1':
  print(s[0])

### FIXED CODE ###

s = list(input())
k = int(input())

if s[0] == '1' and len(s) == 1:
  print(1)
  exit()
elif s[0] != '1':
  print(s[0])
  exit()

if k > 100:
  for i in s:
    if i != '1':
      print(i)
      exit()
  print(1)
  
else:
  for i in s[:k]:
    if i != '1':
      print(i)
      exit()
  print(1)
  exit()
n = int(input())
    l = list(map(int, input().split()))
    def test():
      for i in l:
        if i%2 != 0 and i%3!=0 and i%5!=0:
          return "DENIED"
      return "APPROVED"
    print(test())

### FIXED CODE ###

n = int(input())
l = list(map(int, input().split()))
def test():
  for i in l:
    if i%2 == 0 and i%3!=0 and i%5!=0:
      return "DENIED"
  return "APPROVED"
print(test())
from collections import defaultdict,deque, Counter
import sys,heapq,bisect,math,itertools,string,queue,copy,time
import numpy as np
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
ans = 0

def num_check(n,a,b):
    just_list = list(range(2,n+1))
    ans_dq = deque([1])
    while len(just_list)!=0:
        first_val = just_list[0]
        ans_dq.append(first_val)
        just_list = [i for i in just_list if (i%first_val != 0 and a%i == 0 and b%i == 0)]
    return len(ans_dq)

def gcd(a,b):
    if b>a: return gcd(b,a)
    else: return gcd(b,a%b) if a%b else b

[a,b] = inpl()
n = gcd(a,b)
ans = num_check(int(n),a,b)
print(ans)

### FIXED CODE ###

from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
import numpy as np
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

[A,B] = inpl()

def gcd(a,b):
    if a < b: return gcd(b,a)
    else: 
        return gcd(b,a%b) if b != 0 else a

ans_dq = deque([1])

c = gcd(A,B)
for i in range(2,int(c**0.5)//1+1):
    add_or_not = 0
    while c%i == 0:
        c = c//i
        add_or_not = 1
    if add_or_not: ans_dq.append(i)
if c != 1: ans_dq.append(c)

ans = len(ans_dq)
print(ans)
class UnionFind:
    def __init__(self, n):
        self.par = [-1 for i in range(n+1)]
        self.rank = [0] * (n+1)

    # 検索
    def find(self, x):
        if self.par[x] < 0:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if not x==y: # 根が同じでない場合のみ併合する
            if self.rank[x] < self.rank[y]:
                self.par[y] += self.par[x] # 要素数を併合
                self.par[x] = y # 根を付け替えている
            else:
                self.par[x] += self.par[y]
                self.par[y] = x
                if self.rank[x] == self.rank[y]:
                    self.rank[x] += 1
        
    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y) 

def main():
    import sys
    input = sys.stdin.readline
    N,M = map(int,input().split())
    P = list(map(int,input().split()))
    XY = [list(map(int,input().split())) for i in range(M)]
    A = UnionFind(M)
    
    for i in range(M):
        x,y = XY[i]
        A.union(x,y)
        
    ans = 0
    for i in range(M):
        j = i+1
        p = P[i]
        if A.same_check(j,p):
            ans += 1
    print(ans)
    
    
    
main()

### FIXED CODE ###

class UnionFind:
    def __init__(self, n):
        self.par = [-1 for i in range(n+1)]
        self.rank = [0] * (n+1)

    # 検索
    def find(self, x):
        if self.par[x] < 0:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if not x==y: # 根が同じでない場合のみ併合する
            if self.rank[x] < self.rank[y]:
                self.par[y] += self.par[x] # 要素数を併合
                self.par[x] = y # 根を付け替えている
            else:
                self.par[x] += self.par[y]
                self.par[y] = x
                if self.rank[x] == self.rank[y]:
                    self.rank[x] += 1
        
    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y) 

def main():
    import sys
    input = sys.stdin.readline
    N,M = map(int,input().split())
    P = list(map(int,input().split()))
    XY = [list(map(int,input().split())) for i in range(M)]
    A = UnionFind(N)
    
    for i in range(M):
        x,y = XY[i]
        A.union(x,y)
        
    ans = 0
    for i in range(N):
        j = i+1
        p = P[i]
        if A.same_check(j,p):
            ans += 1
    print(ans)
    
    
    
main()
n = int(input())
xs = list(map(int, input().split()))
ys = list(map(int, input().split()))

print(sum([abs(x-y) for x, y in zip(xs, ys)]))
print(math.sqrt(sum([(x-y)**2 for x, y in zip(xs, ys)])))
print((sum([(x-y)*** for x, y in zip(xs, ys)]))**(1/3))
print(max([abs(x-y) for x, y in zip(xs, ys)]))

### FIXED CODE ###

import math

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

print(sum(map(lambda v: math.fabs(v[0] - v[1]), zip(x, y))))
print(sum(map(lambda v: math.fabs((v[0] - v[1]) ** 2), zip(x, y))) ** (1/2))
print(sum(map(lambda v: math.fabs((v[0] - v[1]) ** 3), zip(x, y))) ** (1/3))
print(max(map(lambda v: math.fabs(v[0] - v[1]), zip(x, y))))
a=input()
b=a[0]
c=a[1]
d=a[2]
e=a[3]
print("SAME" if b==c==d==e else "DIFFERENT" )

### FIXED CODE ###

n=int(input())
print(pow(n,3))
a, b, k = map(int, input().split())
temp = a if a>b else b
list =  [i  for i in range(1,temp) if a%i==0 and b%i==0]
list = list[::-1]
print(list[k-1])

### FIXED CODE ###


a, b, k = map(int, input().split())
temp = a if a>b else b
list =  [i  for i in range(1,temp+1) if a%i==0 and b%i==0]
list = list[::-1]
print(list[k-1])
A,B = map(int, input().split())

if 1 <=A,B >= 9:
    print(A*B)
else:
    print(-1)
    

### FIXED CODE ###

A,B = map(int, input().split())
 
if A<=9 and B<=9:
    print(A*B)
else:
    print(-1)
s = input()
if s == AAA or BBB:
  print('No')
else:
  print('Yes')

### FIXED CODE ###

s = input()
if s == 'AAA' or s == 'BBB':
  print('No')
else:
  print('Yes')