#include<bits/stdc++.h>
using namespace std;const int N=35005,M=105;int n,m,dp[M][N],last[N],a[N],ans,pre[N],ll,rr,nxt[N];
int move(int l,int r){while (rr<r){rr++;if (pre[rr]>=ll)ans+=rr-pre[rr];}while (ll>l){ll--;if (nxt[ll]<=rr)ans+=nxt[ll]-ll;}
	while (rr>r){if (pre[rr]>=ll)ans-=rr-pre[rr];rr--;}while (ll<l){if (nxt[ll]<=rr)ans-=nxt[ll]-ll;ll++;}return ans;}
void solve(int id,int l,int r,int L,int R){if (l>r)return;int mid=(l+r)/2,ID=-1,Min=2e9;
	for (int i=L;i<=min(R,mid);i++)if (dp[id-1][i-1]+move(i,mid)<Min)Min=dp[id-1][i-1]+move(i,mid),ID=i;dp[id][mid]=Min;solve(id,l,mid-1,L,ID);solve(id,mid+1,r,ID,R);}
int main(){scanf("%d%d",&n,&m);memset(dp,0x3f,sizeof dp);for (int i=1;i<=n;i++)scanf("%d",&a[i]);for (int i=1;i<=n;i++){pre[i]=last[a[i]];last[a[i]]=i;}
	for (int i=1;i<=n;i++)last[i]=n+1;for (int i=n;i;i--){nxt[i]=last[a[i]];last[a[i]]=i;}dp[0][0]=0;ll=1;rr=0;for (int i=1;i<=m;i++)solve(i,i,n,1,n);printf("%d\n",dp[m][n]);}