import sys input = lambda : sys.stdin.readline().rstrip() write = lambda x: sys.stdout.write(x+"\n"); writef = lambda x: print("{:.12f}".format(x)) debug = lambda x: sys.stderr.write(x+"\n") YES="Yes"; NO="No"; pans = lambda v: print(YES if v else NO) LI = lambda : list(map(int, input().split())) sys.setrecursionlimit(3*10**5+10) class BIT: ### BIT binary def __init__(self, n, values=None): self.bit = [0]*(n+1) self.n = n self.total = 0 if values is not None: for i,v in enumerate(values): self.add(i,v) self.total += v def check(self): l = [] prv = 0 for i in range(1,self.n+1): val = self.query(i) l.append(val-prv) prv = val print(" ".join(map(str, l))) #a1 ~ aiまでの和 O(logn) def query(self,i): res = 0 while i > 0: res += self.bit[i] # res %= M i -= i&(-i) return res def get(self,i): return self.query(i+1) - self.query(i) #ai += x(logN) def add(self,i,x): i += 1 if i==0: raise RuntimeError self.total += x while i <= self.n: self.bit[i] += x # self.bit[i] %= M i += i&(-i) def index(self, v): """a0,...,aiの和がv以上になる最小のindexを求める 存在しないとき配列サイズを返す """ if v <= 0: return 0 if self.total0: if x+ll>1 return x m,k = LI() a = list(map(int, input().split())) n = len(a) bit = [0]*k count = [0]*m index = [[] for _ in range(m)] vals = [0]*n vs = [0]*n for i in range(n): v = a[i] vs[i] = v+m*count[v] index[v].append(i) val = bit[count[v]] bit[count[v]] += 1 vals[i] = val count[v] += 1 bit2 = BIT(n) total = 0 inv = 0 for v in vs: inv += total - bit2.query(v) bit2.add(v, 1) total += 1 ans = inv for i in range(m): num = 0 for ind in index[i]: num += vals[ind] inv += (-num) + ((m-1)*k-num) ans = min(ans, inv) print(ans)