#include<bits/stdc++.h>
using namespace std;const int N=4e5,M=110,inf=0x3f3f3f3f;
int a[N],dp[N][M],lst[N],pre[N],nxt[N],i,j,n,m,L,R,sum;
int cal(int l,int r){
	while(L<l){if(nxt[L]<=R)sum-=nxt[L]-L;L++;}
	while(L>l){--L;if(nxt[L]<=R)sum+=nxt[L]-L;}
	while(R<r){++R;if(pre[R]>=L)sum+=R-pre[R];}
	while(R>r){if(pre[R]>=L)sum-=R-pre[R];R--;}
	return sum;
} 
void solve(int l,int r,int L,int R,int now){
	if(l>r||L>R)return;int mid=l+r>>1,val=inf,pos;
	for(int i=L;i<mid&&i<=R;++i){
		int tmp=dp[i][now-1]+cal(i+1,mid);
		if(tmp<val)pos=i,val=tmp;
	}dp[mid][now]=val;solve(l,mid-1,L,pos,now);solve(mid+1,r,pos,R,now);
}
int main(){
	for(scanf("%d%d",&n,&m),i=1;i<=n;++i)scanf("%d",a+i);
	for(i=1;i<=n;++i)dp[i][0]=inf,pre[i]=lst[a[i]],lst[a[i]]=i;
	for(i=1;i<=n;++i)lst[a[i]]=n+1;
	for(i=n;i;--i)nxt[i]=lst[a[i]],lst[a[i]]=i;
	for(i=1;i<=m;++i)solve(1,n,0,n,i);printf("%d\n",dp[n][m]);
}