#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define fi first
#define se second
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep0(i, n) for (ll i = 0; i <= (n); ++i)
#define rep1(i, n) for (ll i = 1; i <= (n); ++i)
#define rrep1(i, n) for (ll i = (n); i > 0; --i)
#define rrep0(i, n) for (ll i = (n); i >= 0; --i)
#define srep(i, s, t) for (ll i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define isin(x, l, r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)), x.end())
#define snuke srand((unsigned)clock() + (unsigned)time(NULL));
#define show(x) cerr << #x << " = " << x << endl;
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define bn(x) ((1 << x) - 1)
#define dup(x, y) (((x) + (y)-1) / (y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<T> vt;
constexpr ll MAX = 200000;

int main()
{
    ll n, ans = 0;
    ll sum = 1e10;
    string k;
    cin >> n >> k;
    if (n == 1)
    {
        if (k.at(0) == '0')
            ans = sum;
        else
            ans = 2 * sum;
    }
    else
    {
        if (k.at(0) == '1' && k.at(1) == '1')
        {
            bool ok = true;
            rep(i, n)
            {
                if (i % 3 == 0)
                    if (k.at(i) != '1')
                        ok = false;
                if (i % 3 == 1)
                    if (k.at(i) != '1')
                        ok = false;
                if (i % 3 == 2)
                    if (k.at(i) != '0')
                        ok = false;
            }
            if (ok)
                ans = sum - (n + 2) / 3 + 1;
        }
        else if (k.at(0) == '1' && k.at(1) == '0')
        {
            bool ok = true;
            rep(i, n)
            {
                if (i % 3 == 0)
                    if (k.at(i) != '1')
                        ok = false;
                if (i % 3 == 1)
                    if (k.at(i) != '0')
                        ok = false;
                if (i % 3 == 2)
                    if (k.at(i) != '1')
                        ok = false;
            }
            if (ok)
                ans = sum - (n + 3) / 3 + 1;
        }
        else if (k.at(0) == '0' && k.at(1) == '1')
        {
            bool ok = true;
            rep(i, n)
            {
                if (i % 3 == 0)
                    if (k.at(i) != '0')
                        ok = false;
                if (i % 3 == 1)
                    if (k.at(i) != '1')
                        ok = false;
                if (i % 3 == 2)
                    if (k.at(i) != '1')
                        ok = false;
            }
            if (ok)
                ans = sum - (n + 4) / 3 + 1;
        }
        else
            ans = 0;
    }
    cout << ans << endl;
}
//#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

using namespace std;

//vector string deque break continue
#define forn(i, s, f) for (int i = (int)s; i < (int)f; i++)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair <int, int>
#define fs first
#define sc second
#define pf push_front
#define pb push_back
#define pop_f pop_front
#define pop_b pop_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()

#ifdef DEBUG
#else
    #define cerr if (false) cerr
#endif

template <typename T> istream& operator>>(istream& in, vector <T>& a) {for (auto& i : a) in >> i; return in;}
template <typename T> ostream& operator<<(ostream& out, vector <T>& a)  {for (auto& i : a) out << i << " "; return out;}
template <typename T, typename U> void chkmin(T& a, U b) {if (a > b) a = b;}
template <typename T, typename U> void chkmax(T& a, U b) {if (a < b) a = b;}

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    cin >> a;
    map<int, int> mp;
    forn (i, 0, n) {
        mp[a[i]]++;
    }
    vector<vector<int>> boxes(k);
    for (auto [val, cnt] : mp) {
        int lst = 0;
        forn (i, 0, cnt) {
            boxes[lst].pb(val);
            lst = (lst + 1) % k;
        }
    }
    long long ans = 0;
    forn (i, 0, k) {
        vector<bool> used(boxes[i].size() + 1);
        for (int j : boxes[i])
            if (j < used.size())
                used[j] = true;
        forn (j, 0, sz(used)) {
            if (!used[j]) {
                ans += j;
                break;
            }
        }
    }
    cout << ans << "\n";
    return 0;
}
