Takahashi has N balls, each initially labeled with an integer Ai. He wants to rewrite some labels so that there are at most K different integers on the N balls. Find the minimum number of balls he needs to rewrite. Constraints: 1 ≤ K ≤ N ≤ 200000, 1 ≤ Ai ≤ N. Input format: N K, followed by A1 to AN. Output: Print the minimum number of balls to rewrite. Example: Input 5 2, with labels 1 1 2 2 5, should output 1.

# Buggy version:
from collections import Counter 

N, K = map(int, input().split())
A = list(map(int, input().split()))
s = len(set(A))
t = s-K

c = Counter(A)
num = list(c)
num.sort()
print(num,t)
print(sum(num[:t])) if t>0 else print(0)

# Fixed version:
from collections import Counter 

N, K = map(int, input().split())
A = list(map(int, input().split()))
s = len(set(A))
t = s-K

c = Counter(A)
num = list(c.values())
num.sort()

print(sum(num[:t])) if t>0 else print(0)

# Buggy version:
from collections import defaultdict
N, K = map(int,input().split())
A = list(map(int,input().split()))
D = defaultdict(int)
for a in A:
  D[a]+=1
D = sorted(D.values())
ans = 0
if len(D)<=K:
  print(ans)
else:
    print(sum(D[:K]))

# Fixed version:
from collections import defaultdict
N, K = map(int,input().split())
A = list(map(int,input().split()))
D = defaultdict(int)
for a in A:
  D[a]+=1
D = sorted(D.values())
ans = 0
if len(D)<=K:
  print(ans)
else:
    print(sum(D[:len(D)-K]))

# Buggy version:
from collections import Counter
N,K=map(int,input().split())
N=list(map(int,input().split()))
A=Counter(N)
C=list(A.values())
if len(C)<=K:
    print(0)
else:
    ans=sum(C[-(len(C)-K):])
    print(ans)

# Fixed version:
from collections import Counter
N,K=map(int,input().split())
N=list(map(int,input().split()))
A=Counter(N)
C=list(A.values())
C.sort(reverse=True)
if len(C)<=K:
    print(0)
else:
    ans=sum(C[-(len(C)-K):])
    print(ans)

# Buggy version:
n,k = map(int, input().split())
num = [0] * 200000
an = list(map(int, input().split()))
for i in range(len(an)):
    num[an[i]] += 1
Ans = 0
num2 = [s for s in num if s != 0]
num2.sort()
length = len(num2)
for i in range(length):
    Ans += num2[i]
    length = length - 1
    if length == k:
        break
print(Ans)

# Fixed version:
n,k = map(int, input().split())
num = [0] * 200000
an = list(map(int, input().split()))
for i in range(len(an)):
    num[an[i]] += 1
Ans = 0
num2 = [s for s in num if s != 0]
num2.sort()
length = len(num2)
if k < length:
    for i in range(length):
        Ans += num2[i]
        length = length - 1
        if length == k:
            break
print(Ans)

# Buggy version:
N, K=map(int, input().split())
A=list(map(int, input().split()))
sa=set(A)
if len(sa)<=K:
  print(0)
else:
  # 書き換える要素の種類数
  sub=len(sa)-K
  # 少ないやつを書き換えたい
  A.sort()
  A.append(A[-1])
  a=[0]*len(sa)
  j=0
  for i in range(N):
    a[j]+=1
    if A[i]!=A[i+1]:
      j+=1
  
  a.sort()

# Fixed version:
N, K=map(int, input().split())
A=list(map(int, input().split()))
sa=set(A)
if len(sa)<=K:
  print(0)
else:
  # 書き換える要素の種類数
  sub=len(sa)-K
  # 少ないやつを書き換えたい
  A.sort()
  A.append(A[-1])
  a=[0]*len(sa)
  j=0
  for i in range(N):
    a[j]+=1
    if A[i]!=A[i+1]:
      j+=1
  
  a.sort()
  print(sum(a[:sub]))

# Buggy version:
from collections import Counter

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

C = Counter(A).most_common()
L = len(C)

if L <= K:
  print(0)
  exit()

ans = 0
cnt = 0
for v in C.values():
  cnt += 1
  if K < cnt:
    ans += v
    
print(ans)


# Fixed version:
from collections import Counter

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

C = Counter(A).most_common()
L = len(C)

if L <= K:
  print(0)
  exit()

ans = 0
cnt = 0
for k,v in C:
  cnt += 1
  if K < cnt:
    ans += v
    
print(ans)

# Buggy version:
from collections import Counter

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

li = Counter(A)
li = list(li.values())
li.sort()

sum(li[:max(len(li) - K, 0)])

# Fixed version:
from collections import Counter

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

li = Counter(A)
li = list(li.values())
li.sort()

print(sum(li[:max(len(li) - K, 0)]))

# Buggy version:
import sys
from collections import Counter
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


def main():
    N, K = map(int, readline().split())
    A = list(map(int, readline().split()))
    count = Counter(A)
    L = len(count)
    print(L)
    if L<=K:
        print(0)
    else:
        B = list(count.values())
        B.sort()
        print(sum(B[:L-K]))



if __name__ == '__main__':
    main()

# Fixed version:
import sys
from collections import Counter
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


def main():
    N, K = map(int, readline().split())
    A = list(map(int, readline().split()))
    count = Counter(A)
    L = len(count)
    if L<=K:
        print(0)
    else:
        B = list(count.values())
        B.sort()
        print(sum(B[:L-K]))



if __name__ == '__main__':
    main()

# Buggy version:
n, k = map(int, input().split())
A = list(map(int, input().split()))

import collections
c = collections.Counter(A)
print(c)
if len(c.keys()) > k:
    num = len(c.keys()) - k
    print(sum(sorted(c.values())[:num]))
else:
    print(0)

# Fixed version:
n, k = map(int, input().split())
A = list(map(int, input().split()))

import collections
c = collections.Counter(A)
#print(c)
if len(c.keys()) > k:
    num = len(c.keys()) - k
    print(sum(sorted(c.values())[:num]))
else:
    print(0)

# Buggy version:
import collections
n, k = map(int, input().split())
a = list(map(int, input().split()))
t = 0
ans = n

l = collections.Counter(a)
x = l.most_common(k)

for i in range(k):
    t += x[i][1]

ans -= t
if ans < 0:
    ans = 0
print(ans)


# Fixed version:
import collections
n, k = map(int, input().split())
a = list(map(int, input().split()))
t = 0
ans = n

l = collections.Counter(a)
x = l.most_common(k)

for i in range(len(x)):
    t += x[i][1]

ans -= t
if ans < 0:
    ans = 0
print(ans)

# Buggy version:
N,K=map(int,input().split())
a=input().split()

list.sort(a)#小さい順に並び替える
    
count=1#各数字が何回出てくるか
size=0#何種類の数字が出てくるか
b=[]#各数字が何回出てきたかを入れる箱
for p in range (N-1):
  if a[p]==a[p+1]:
    count+=1
  else:
    b.append(count)
    count=1
    size+=1

b.append(count)
size+=1
  
print(b)
list.sort(b,reverse=True)#大きい順に並び替える

s=0
if K<size:
  for u in range (K):
    s+=b[u]#大きい方からK個分の個数の合計
else:
  s=N#出てくる数の個数が少なければ変える必要はない
print(N-s)

# Fixed version:
N,K=map(int,input().split())
a=input().split()

list.sort(a)#小さい順に並び替える
    
count=1#各数字が何回出てくるか
size=0#何種類の数字が出てくるか
b=[]#各数字が何回出てきたかを入れる箱
for p in range (N-1):
  if a[p]==a[p+1]:
    count+=1
  else:
    b.append(count)
    count=1
    size+=1

b.append(count)
size+=1
  
list.sort(b,reverse=True)#大きい順に並び替える

s=0
if K<size:
  for u in range (K):
    s+=b[u]#大きい方からK個分の個数の合計
else:
  s=N#出てくる数の個数が少なければ変える必要はない
print(N-s)

# Buggy version:
n , k = map(int, input().split())
a = list(map(int, input().split()))

dict = {}
for i in range(n):
  if a[i] in dict:
    dict[a[i]] += 1
  else:
    dict[a[i]] = 1
ans = sorted(dict.items(), key=lambda x:x[1])

if len(ans) == k:
  print(0)
else:
  tmp = 0
  s = 0
  for i in ans:
    tmp += i[1]
    s+=1
    if s == len(dict) - k:
      break
  print(tmp)
    

# Fixed version:
n , k = map(int, input().split())
a = list(map(int, input().split()))

dict = {}
for i in range(n):
  if a[i] in dict:
    dict[a[i]] += 1
  else:
    dict[a[i]] = 1
ans = sorted(dict.items(), key=lambda x:x[1])
if len(ans) <= k:
  print(0)
else:
  tmp = 0
  s = 0
  for i in ans:
    tmp += i[1]
    s+=1
    if s == len(dict) - k:
      break
  print(tmp)

# Buggy version:
from collections import Counter
n, k = map(int, input().split())
al = sorted(Counter(input().split()).values(), reverse=True)

print(al)
print(sum(al[k:]))


# Fixed version:
from collections import Counter
n, k = map(int, input().split())
al = sorted(Counter(input().split()).values(), reverse=True)
print(sum(al[k:]))

# Buggy version:
N,K=map(int,input().split())
A=list(map(int,input().split()))

B=[0]*(N+1)
for i in range(0,N):
    B[A[i]]=B[A[i]]+1
C=[]
for i in range(0,N+1):
    if B[i]!=0:
        C.append(B[i])
print(C)
C.sort()
print(C)
if K>=len(C):
    print(0)
else:
    s=0
    for i in range(0,len(C)-K):
        s=s+C[i]
    print(s)


# Fixed version:
N,K=map(int,input().split())
A=list(map(int,input().split()))

B=[0]*(N+1)
for i in range(0,N):
    B[A[i]]=B[A[i]]+1
C=[]
for i in range(0,N+1):
    if B[i]!=0:
        C.append(B[i])
C.sort()
if K>=len(C):
    print(0)
else:
    s=0
    for i in range(0,len(C)-K):
        s=s+C[i]
    print(s)

# Buggy version:
n,k = map(int, input().split( ))
import sys
from collections import Counter
input = sys.stdin.readline
a = list(map(int,input().split( )))

ac = Counter(a)
cnt = []
for k in ac:
    cnt.append(ac[k])
cnt.sort()
if len(cnt)<=2:
    print(0)
else:
    print(sum(cnt[:-2]))

# Fixed version:
n,k = map(int, input().split( ))
import sys
from collections import Counter
input = sys.stdin.readline
a = list(map(int,input().split( )))

ac = Counter(a)
cnt = []
for ke in ac:
    cnt.append(ac[ke])
cnt.sort()
if len(cnt)<=k:
    print(0)
else:
    print(sum(cnt[:-k]))

# Buggy version:
import collections
import sys
N,K = map(int,input().split())
A = list(map(int,input().split()))
c = collections.Counter(A)
if len(c)<= K:
  print(0)
  sys.exit()
for i in range(K):
    cnt += c.most_common()[i][1]
    if cnt >= K:

print(N-cnt)



# Fixed version:
import collections
import sys

N,K = map(int,input().split())
A = list(map(int,input().split()))
c = collections.Counter(A)
cnt = 0
if len(c)<= K:
    print(0)
    sys.exit()

for i in c.most_common(K):
    cnt += i[1]

print(N-cnt)

# Buggy version:
n=map(int,input().split())
t= list(map(int,input().split()))

import sys
import collections
c = collections.Counter(t)
c=sorted(list(c.values()))
ans=0
delete_count=len(c)-k
if delete_count<=0:
    print(0)
else:
    for i in range(delete_count):
        if len(c)>k:
            ans+=c[i]
            
print(ans)
sys.exit()
        



# Fixed version:
n,k=map(int,input().split())
t= list(map(int,input().split()))

import sys
import collections
c = collections.Counter(t)
c=sorted(list(c.values()))
ans=0
delete_count=len(c)-k
if delete_count<=0:
    print(0)
    sys.exit()
else:
    for i in range(delete_count):
        if len(c)>k:
            ans+=c[i]
            
print(ans)
sys.exit()

# Buggy version:
import collections
N,K = map(int,input().split())
A = list(map(int,input().split()))
c = collections.Counter(A).most_common()[::-1]
print(c)
k = len(c)
if k<=K:print(0)
else: print(sum([e[1] for e in c[:k-K]]))

# Fixed version:
import collections
N,K = map(int,input().split())
A = list(map(int,input().split()))
c = collections.Counter(A).most_common()[::-1]
k = len(c)
if k<=K:print(0)
else: print(sum([e[1] for e in c[:k-K]]))

# Buggy version:
from collections import defaultdict

n,k = map(int,input().split())
a_li = list(map(int,input().split()))

d = defaultdict(int)

for key in a_li:
    d[key] += 1
d = sorted(d.items(), key=lambda x:x[1])

if k >= len(d):
    print(0)
    
s = len(d) - k
ans = 0

for i in range(s):
    ans += d[i][1]
    
print(ans)

# Fixed version:
from collections import defaultdict

n,k = map(int,input().split())
a_li = list(map(int,input().split()))

d = defaultdict(int)

for key in a_li:
    d[key] += 1
d = sorted(d.items(), key=lambda x:x[1])
    
s = len(d) - k
ans = 0

for i in range(s):
    ans += d[i][1]
    
print(ans)

# Buggy version:
from collections import defaultdict
n,k = map(int,input().split())
nums = input().split()
count_per_num = defaultdict(int)
for n in nums:
  count_per_num[n] += 1
count_per_nums = sorted(count_per_nums, key=lambda x:x[1])
ans = 0
for i in range(len(count_per_nums)-k):
  ans += count_per_nums[i]
print(ans)

# Fixed version:
from collections import defaultdict
n,k = map(int,input().split())
nums = input().split()
count_per_nums = defaultdict(int)
for n in nums:
  count_per_nums[n] += 1
count_per_nums = sorted(count_per_nums.items(), key=lambda x:x[1])
ans = 0
for i in range(len(count_per_nums)-k):
  ans += count_per_nums[i][1]
print(ans)

# Buggy version:
from collections import Counter

n, k = map(int, input().split())
A = sorted(Counter(list(map(int, input().split()))).most_common(), key=lambda x:x[1])
cnt = 0
print(A)
if len(A) > k:
    for i in range(len(A)-k):
        cnt += A[i][1]
print(cnt)

# Fixed version:
from collections import Counter

n, k = map(int, input().split())
A = sorted(Counter(list(map(int, input().split()))).most_common(), key=lambda x:x[1])
cnt = 0
if len(A) > k:
    for i in range(len(A)-k):
        cnt += A[i][1]
print(cnt)

# Buggy version:
from collections import Counter
N, K = map(int, input().split())
A = list(map(int, input().split()))
dA = Counter(A)
if len(dA) - K <= 0:
  print(0)
  exit()
print(sum(sorted(dA.values())[:len(dA)-K])))

# Fixed version:
from collections import Counter
N, K = map(int, input().split())
A = list(map(int, input().split()))
dA = Counter(A)
if len(dA) - K <= 0:
  print(0)
  exit()
print(sum(sorted(dA.values())[:len(dA)-K] ))

# Buggy version:
n,k=map(int,input().split())
a_list=[int(i) for i in input().split()]
count={}
for a in a_list:
    count[a]=count.get(a,0)+1
val=list(count.values())
sorted(val,reverse=True)
print(sum(val[k:]))

# Fixed version:
n,k=map(int,input().split())
a_list=[int(i) for i in input().split()]
count={}
for a in a_list:
    count[a]=count.get(a,0)+1
val=list(count.values())
val.sort(reverse=True)
print(sum(val[k:]))

# Buggy version:
n,k=map(int,input().split())
d=dict()
for i in map(int,input().split()):
  if i not in d:
    d[i]=1
  else:
    d[i]+=1
m=list(d.keys())
m.sort()
a=len(d.values())-k
ans=0
for i in m:
  if a<=0:
    break
  a-=1
  ans+=1
print(ans)

# Fixed version:
n,k=map(int,input().split())
d=dict()
for i in map(int,input().split()):
  if i not in d:
    d[i]=1
  else:
    d[i]+=1
m=list(d.values())
m.sort()
a=len(m)-k
ans=0
for i in m:
  if a<=0:
    break
  a-=1
  ans+=i
print(ans)

# Buggy version:
from collections import Counter
N,K = map(int,input().split())
A = list(map(int,input().split()))
c = Counter(A)
values_list = list(c.values())
values_list.reverse()
ans = 0
for i in range(len(values_list)-K):
    ans += values_list[i]
print(ans)

# Fixed version:
from collections import Counter
N,K = map(int,input().split())
A = list(map(int,input().split()))
c = Counter(A)
values_list = list(c.values())
values_list.sort()
ans = 0
for i in range(len(values_list)-K):
    ans += values_list[i]
print(ans)

# Buggy version:
from collections import Counter
import sys

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

if k>=len(c):
    print(0)
    sys.exit()
d = c.values()
print(sorted(d))
print(sum(sorted(d)[:len(c)-k]))


# Fixed version:
from collections import Counter
import sys

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

if k>=len(c):
    print(0)
    sys.exit()
d = c.values()
print(sum(sorted(d)[:len(c)-k]))

# Buggy version:
import math
import sys
from collections import Counter
def getinputdata():

    # 配列初期化
    array_result = []
    
    data = input()

    
    array_result.append(data.split(" "))

    flg = 1

    try:


        while flg:

            data = input()

            array_temp = []

            if(data != ""):
                
                array_result.append(data.split(" "))

                flg = 1

            else:

                flg = 0
    finally:


        return array_result





arr_data = getinputdata()

n = int(arr_data[0][0])
k = int(arr_data[0][1])

arr=arr_data[1]


list01=Counter(arr)

#print(list01,len(list01))

#print(list01)

cnt=0
if len(list01) > k:
    
    for key,val in sorted(list01.items(),reverse=True):

        print(key,val,len(list01))

        cnt+=list01.pop(key)
        
        
        if len(list01)==k:
            
            break
        

print(cnt)


# Fixed version:
import math
import sys
from collections import Counter
def getinputdata():

    # 配列初期化
    array_result = []
    
    data = input()

    
    array_result.append(data.split(" "))

    flg = 1

    try:


        while flg:

            data = input()

            array_temp = []

            if(data != ""):
                
                array_result.append(data.split(" "))

                flg = 1

            else:

                flg = 0
    finally:


        return array_result





arr_data = getinputdata()

n = int(arr_data[0][0])
k = int(arr_data[0][1])

arr=arr_data[1]


list01=Counter(arr)

#print(list01,len(list01))

#print(list01)

cnt=0
if len(list01) > k:
    
    for key,val in sorted(list01.items(),key=lambda x: x[1]):

#        print(key,val,len(list01))

        cnt+=list01.pop(key)
        
        
        if len(list01)==k:
            
            break
        

print(cnt)

# Buggy version:
lis2 = list(map(int, input().split()))
n = lis[0]
k = lis[1]
d = {}

for i in range(n):
    if lis2[i] not in d.keys():
       d[lis2[i]] = 1
    else:
        d[lis2[i]] += 1 
        
if len(b) <= k:
    print(0)
else:
    a = d.values()
    a.sort()
    a.reverse()
    swap = 0
    for i in range(k,len(a)):
        swap += a[i]
    print(swap)

# Fixed version:
lis = list(map(int, input().split()))
lis2 = input().split()
n = lis[0]
k = lis[1]
d = {}

for i in range(n):
    if lis2[i] not in d.keys():
       d[lis2[i]] = 1
    else:
        d[lis2[i]] += 1 
        
if len(list(d.values())) <= k:
    print(0)
else:
    a = list(d.values())
    a.sort()
    a.reverse()
    swap = 0
    for i in range(k,len(a)):
        swap += a[i]
    print(swap)

# Buggy version:
n,k=map(int,input().split())
a=list(map(int,input().split()))
import collections
count=0
data=collections.Counter(a)
data=sorted(data.items(),key=lambda x:x[1])
if k<len(data):
    for i in range(len(data)-k):
        count+=data[i][1]
else:
    print(0)
print(count)


# Fixed version:
n,k=map(int,input().split())
a=list(map(int,input().split()))
import collections
count=0
data=collections.Counter(a)
data=sorted(data.items(),key=lambda x:x[1])
if k<len(data):
    for i in range(len(data)-k):
        count+=data[i][1]
else:
    print(0)
    exit()
print(count)

# Buggy version:
from collections import OrderedDict

temp=input()
n,k=list(map(int,temp.split(" ")))
co=OrderedDict()
ans=[]
temp=input()
nums=list(map(int,temp.split(" ")))
for i in nums:
  if i in co:
	  co[i]+=1
  else:
	  co[i]=1
for i in co:
  ans.append(co[i])
kinds=len(ans)
dif=kinds-k
if dif<=0:
    print(0)
else:
    ans.sort()
    print(sum(ans[:dif-1]))

# Fixed version:
from collections import OrderedDict

temp=input()
n,k=list(map(int,temp.split(" ")))
co=OrderedDict()
ans=[]
temp=input()
nums=list(map(int,temp.split(" ")))
for i in nums:
  if i in co:
	  co[i]+=1
  else:
	  co[i]=1
for i in co:
  ans.append(co[i])
kinds=len(ans)
dif=kinds-k
if dif<=0:
    print(0)
else:
    ans.sort()
    print(sum(ans[:dif]))

# Buggy version:
from collections import defaultdict

n, k = map(int, input().split())
al = [int(i) for i in range(n)]

# dictionary で種類と数値をとっておく
d = defaultdict(int)
for a in al:
  d[a] += 1

# 種類が少ないものから色を変えていくと最小の変えなければいけないボールの数がわかる
ans = 0
if d.keys() > k:
  # 色を塗り替えないといけないので数えなければ…
  v = d.values()
  v.sort()
  ans = sum(v[:d.keys() - k])
print(ans)

# Fixed version:
from collections import defaultdict

n, k = map(int, input().split())
al = [int(i) for i in input().split()]

# dictionary で種類と数値をとっておく
d = defaultdict(int)
for a in al:
  d[a] += 1

# 種類が少ないものから色を変えていくと最小の変えなければいけないボールの数がわかる
ans = 0
if len(d.keys()) > k:
  # 色を塗り替えないといけないので数えなければ…
  v = list(d.values())
  v.sort()
  ans = sum(v[:len(d.keys()) - k])
print(ans)

# Buggy version:
from collections import Counter

n,k = map(int, input().split())
a = [input() for _ in range(n)]
c = Counter(a)
t = len(c) - k
r = 0

if t > 0:
    serial = list(map(list, c.most_common[-t:]))
    for s in serial:
        r += s[1]

print(r)

# Fixed version:
from collections import Counter

n,k = map(int, input().split())
a = input().split()
c = Counter(a)
t = len(c) - k
r = 0

if t > 0:
    serial = c.most_common()[-t:]
    for s in serial:
        r += s[1]

print(r)

# Buggy version:
N, K = map(int, input().split())
A = map(int, input().split())

cnt = {}

for a in A:
    if a not in cnt:
        cnt[a] = 0
    cnt[a] += 1

cnt = sorted(cnt.items(), key=lambda x: x[1])
ans = 0
i = 0

for i in range(len(cnt)):
    if len(cnt) + i <= K:
        break
    ans += cnt[i][1]

print(ans)


# Fixed version:
N, K = map(int, input().split())
A = map(int, input().split())

cnt = {}

for a in A:
    if a not in cnt:
        cnt[a] = 0
    cnt[a] += 1

cnt = sorted(cnt.items(), key=lambda x: x[1])
ans = 0

for i in range(len(cnt)):
    if len(cnt) - i <= K:
        break
    ans += cnt[i][1]

print(ans)