
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int M = 2000000000;

struct ST
{
    int n;
    vector<int> lazy;
    vector<int> tree;

    ST(int n1)
    {
        n = n1;
        lazy.assign(4 * n, 0);
        tree.assign(4 * n, 0);
    }

    void clean()
    {
        lazy.assign(4 * n, 0);
        tree.assign(4 * n, 0);
    }

    void push(int node)
    {
        for (int i = node * 2; i <= node * 2 + 1; i++)
        {
            tree[i] += lazy[node];
            lazy[i] += lazy[node];
        }
        lazy[node] = 0;
    }

    void add(int num, int from, int to, int a, int b, int node)
    {
        if (from <= a && b <= to)
        {
            tree[node] += num;
            lazy[node] += num;
            return;
        }

        push(node);
        int mid = (a + b) / 2;
        if (from <= mid) add(num, from, to, a, mid, node * 2);
        if (to > mid) add(num, from, to, mid + 1, b, node * 2 + 1);
        tree[node] = min(tree[node * 2], tree[node * 2 + 1]);
    }

    void add(int num, int from, int to)
    {
        add(num, from, to, 0, n - 1, 1);
    }

    int getMin(int from, int to, int a, int b, int node)
    {
        if (from <= a && b <= to) return tree[node];

        push(node);
        int mid = (a  +b) / 2;
        int res = M;
        if (from <= mid) res = min(res, getMin(from, to, a, mid, node * 2));
        if (to > mid) res = min(res, getMin(from, to, mid + 1, b, node * 2 + 1));

        return res;
    }

    int getMin(int from, int to)
    {
        return getMin(from, to, 0, n - 1, 1);
    }

};

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];


    vector<vector<int>> dp(n, vector<int>(k + 1));



    vector<int> last(n + 1, -1);
    vector<int> prev(n);
    for (int i = 0; i < n; i++)
    {
        prev[i] = last[a[i]];
        if (i != 0) dp[i][1] = dp[i - 1][1];
        if (prev[i] != -1)
        {
            dp[i][1] += i - prev[i];
        }
        last[a[i]] = i;
    }

    ST tree(n);
    for (int k1 = 2; k1 <= k; k1++)
    {
        tree.clean();
        for (int i = 0; i < n; i++)
        {
            tree.add(dp[i][k1 - 1], i, i);
        }

        for (int i = 0; i < n; i++)
        {
            if (prev[i] != -1  && prev[i] != 0)
            {
                tree.add(i - prev[i], 0, prev[i] - 1);
            }

            if (i == 0)dp[i][k1] = 0;
            else
            {
                dp[i][k1] = tree.getMin(0, i - 1);
            }
        }
    }



    cout << dp[n - 1][k] << "\n";
}
