#include <bits/stdc++.h>
#define For(i, a, b) for(int i = a; i <= b; i++)
#define Ford(i, a, b) for(int i = a; i >= b; i--)
#define make_pair(a,b) mp(a, b)
 
using namespace std;
const int N= 2e5 + 1067, K = 17, md=1e9 + 7;
int n, k;
int f[N][30], g[1 << K], dem[30];
string s;

bool check(int x) {
    int en = n - 1;
    for(char j = 'a'; j < 'a' + k; j++) f[n][j] = f[n + 1][j] = n, dem[j] = 0;
    Ford(i, n - 1, 0) {
        for(char j = 'a'; j < 'a' + k; j++) 
            f[i][j] = f[i + 1][j];
        ++dem[s[i]];
        if (en >= i + x) {
            --dem[s[en]];
            en -= 1;
        }
        char c = '-';
        int diversity = 0;
        for(char j = 'a'; j < 'a' + k; j++) {
            if (dem[j]) {
                diversity += 1;
                c = j;
            }
        }
        if (diversity == 0) {
            for(char j = 'a'; j < 'a' + k; j++) 
                f[i][j] = i + x - 1;
        } else if (diversity == 1) {
            f[i][c] = i + x - 1;
        }
    }
    
    For(mask, 0, (1 << k) - 1) g[mask] = n;
    g[0] = -1;
    For(mask, 0, (1 << k) - 1) {
        For(i, 0, k - 1) if (!(mask & (1 << i))) {
            g[mask ^ (1 << i)] = min(g[mask ^ (1 << i)], f[g[mask] + 1][(char)('a' + i)]); 
        }
    }
    
    return g[(1 << k) - 1] < n;
}

int bs(int l, int r) {
    while (l < r) {
        if (l == r - 1) {
            if (check(r)) 
                return r;
            else 
                return l;
        }
        int mid = (l + r) / 2;
        if (check(mid)) 
            l = mid;
        else 
            r = mid - 1;
    }
    return l;
} 

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> k;
    cin >> s;
    cout << bs(0, n) << endl;
}