#include <bits/stdc++.h>

using namespace std;
int n,k,l,r,m,i,j,l1,l2,x,b[20][200004],mask[140004];
char c,s[200004];
int main()
{
    scanf ("%d %d", &n, &k);
    scanf ("%s", &s);
    l=0;
    r=n;
    while (l<=r)
    {
        m=(l+r)/2;
        for (i=0;i<k;i++)
            for (j=0;j<=n;j++)
                b[i][j]=n;
        for (i=0;i<(1<<k);i++)
            mask[i]=n;
        mask[0]=-1;
        c=' ';
        l1=-1;
        l2=-1;
        for (i=0;i<n;i++)
        {
            if (s[i]=='?')
            {
                if ((l1+m)<=i)
                {
                    for (j=0;j<k;j++)
                        b[j][i]=i;
                }
                else if ((l2+m)<=i)
                    b[c-'a'][i]=i;
            }
            else
            {
                if (s[i]==c)
                {
                    if ((l2+m)<=i)
                        b[s[i]-'a'][i]=i;
                    l1=i;
                }
                else
                {
                    if ((l1+m)<=i)
                        b[s[i]-'a'][i]=i;
                    l2=l1;
                    l1=i;
                    c=s[i];
                }
            }
        }
        for (i=0;i<k;i++)
            for (j=(n-1);j>=0;j--)
                b[i][j]=min(b[i][j],b[i][j+1]);
        for (i=1;i<(1<<k);i++)
        {
            for (j=0;j<k;j++)
            {
                if ((i&(1<<j))!=0)
                {
                    x=mask[i-(1<<j)]+m;
                    if (x<=n)
                        mask[i]=min(mask[i],b[j][x]);
                }
            }
        }
        if (mask[(1<<k)-1]<n)
            l=m+1;
        else
            r=m-1;
    }
    printf ("%d\n", r);
    return 0;
}
