using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("7 2");
WillReturn.Add("1");
WillReturn.Add("2");
WillReturn.Add("2");
WillReturn.Add("2");
WillReturn.Add("1");
WillReturn.Add("2");
WillReturn.Add("1");
//2
}
else if (InputPattern == "Input2") {
WillReturn.Add("12 4");
WillReturn.Add("1");
WillReturn.Add("3");
WillReturn.Add("2");
WillReturn.Add("4");
WillReturn.Add("2");
WillReturn.Add("1");
WillReturn.Add("2");
WillReturn.Add("3");
WillReturn.Add("1");
WillReturn.Add("1");
WillReturn.Add("3");
WillReturn.Add("4");
//7
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
// 個数[ぬいぐるみの種類]なDict
static Dictionary<long, long> mCntDict;
static long mM;
static long AllBitOn;
static List<long> mBitList = new List<long>();
static void Main()
{
List<string> InputList = GetInputList();
long[] wkArr = InputList[0].Split(' ').Select(pX => long.Parse(pX)).ToArray();
mM = wkArr[1];
AllBitOn = (1 << (int)mM) - 1;
long Beki2Val = 1;
for (long I = 1; I <= mM; I++) {
mBitList.Add(Beki2Val);
Beki2Val *= 2;
}
long[] KindArr = InputList.Skip(1).Select(pX => long.Parse(pX)).ToArray();
long UB = KindArr.GetUpperBound(0);
// フェニック木[ぬいぐるみの種類]なDict
var Fenwick_Tree_Dict = new Dictionary<long, Fenwick_Tree>();
for (long I = 1; I <= mM; I++) {
Fenwick_Tree_Dict[I] = new Fenwick_Tree(UB);
}
for (long I = 0; I <= UB; I++) {
long CurrKind = KindArr[I];
Fenwick_Tree_Dict[CurrKind].Add(I, 1);
}
mCntDict = KindArr.GroupBy(pX => pX).ToDictionary(pX => pX.Key, pX => pX.LongCount());
// DFSでBitSetごとの長さを求める
ExecDFS();
// 最小コスト[BitSet]なインラインDP表
long?[] DPArr = new long?[AllBitOn + 1];
DPArr[0] = 0;
for (long I = 0; I <= AllBitOn; I++) {
if (DPArr[I].HasValue == false) continue;
// 配置するぬいぐるみでループ
for (int J = 0; J <= mBitList.Count - 1; J++) {
if ((I & mBitList[J]) > 0) {
continue;
}
long NewI = I + mBitList[J];
long NewVal = DPArr[I].Value;
long RangeSta = mBitSetInfoDict[I];
long RangeEnd = mBitSetInfoDict[I] + mCntDict[J + 1] - 1;
long BeforeCnt = Fenwick_Tree_Dict[J + 1].GetSum(RangeSta, RangeEnd);
NewVal += (RangeEnd - RangeSta + 1) - BeforeCnt;
if (DPArr[NewI].HasValue) {
if (DPArr[NewI].Value <= NewVal) {
continue;
}
}
DPArr[NewI] = NewVal;
}
//Console.WriteLine("I={0}でのDP結果", I);
//for (long J = 0; J <= AllBitOn; J++) {
// if (DPArr[J].HasValue == false) continue;
// Console.WriteLine("DPArr[{0}]={1}", J, DPArr[J]);
//}
}
Console.WriteLine(DPArr[AllBitOn]);
}
// ぬいぐるみの総数[BitSet]なDict
static Dictionary<long, long> mBitSetInfoDict = new Dictionary<long, long>();
// DFSでBitSetごとの長さを求める
struct JyoutaiDef
{
internal int CurrInd;
internal long BitSet;
internal long LengthSum;
}
static void ExecDFS()
{
var Stk = new Stack<JyoutaiDef>();
JyoutaiDef WillPush;
WillPush.CurrInd = 0;
WillPush.BitSet = 0;
WillPush.LengthSum = 0;
Stk.Push(WillPush);
while (Stk.Count > 0) {
JyoutaiDef Popped = Stk.Pop();
mBitSetInfoDict[Popped.BitSet] = Popped.LengthSum;
for (int I = Popped.CurrInd; I <= mBitList.Count - 1; I++) {
WillPush.CurrInd = I + 1;
WillPush.BitSet = Popped.BitSet + mBitList[I];
WillPush.LengthSum = Popped.LengthSum + mCntDict[I + 1];
Stk.Push(WillPush);
}
}
}
}
// フェニック木
#region Fenwick_Tree
internal class Fenwick_Tree
{
private long[] mBitArr;
private long mExternalArrUB;
// ノードのIndexの列挙を返す
internal IEnumerable<long> GetNodeIndEnum()
{
for (long I = 0; I <= mExternalArrUB; I++) {
yield return I;
}
}
// 木のノードのUBを返す
internal long GetUB()
{
return mExternalArrUB;
}
// コンストラクタ(外部配列のUBのみ指定)
internal Fenwick_Tree(long pExternalArrUB)
{
mExternalArrUB = pExternalArrUB;
// フェニック木の外部配列は0オリジンで、
// フェニック木の内部配列は1オリジンなため、2を足す
mBitArr = new long[pExternalArrUB + 2];
}
// コンストラクタ(初期化用の配列指定)
internal Fenwick_Tree(long[] pArr)
: this(pArr.GetUpperBound(0))
{
for (long I = 0; I <= pArr.GetUpperBound(0); I++) {
this.Add(I, pArr[I]);
}
}
// コンストラクタ(初期化用のList指定)
internal Fenwick_Tree(List<long> pList)
: this(pList.Count - 1)
{
for (int I = 0; I <= pList.Count - 1; I++) {
this.Add(I, pList[I]);
}
}
// [pSta,pEnd] のSumを返す
internal long GetSum(long pSta, long pEnd)
{
return GetSum(pEnd) - GetSum(pSta - 1);
}
// [0,pEnd] のSumを返す
internal long GetSum(long pEnd)
{
pEnd++; // 1オリジンに変更
long Sum = 0;
while (pEnd >= 1) {
Sum += mBitArr[pEnd];
pEnd -= pEnd & -pEnd;
}
return Sum;
}
// [I] に Xを加算
internal void Add(long pI, long pX)
{
pI++; // 1オリジンに変更
while (pI <= mBitArr.GetUpperBound(0)) {
mBitArr[pI] += pX;
pI += pI & -pI;
}
}
}
#endregion