#include <cstdio>
#include <cstring>

using namespace std;

const int MaxN = 300000 + 5;
const int MaxM = 300000 + 5;

int N, M, Sum[MaxN];
char Str[MaxN];

inline int Solve1 ( void )
{
    return -1;
}

inline int Solve2 ( void )
{
    int CntL[3005], PrevL[3005];
    bool CanL[3005];

    memset ( CntL, 0, sizeof ( CntL ) );
    memset ( PrevL, -1, sizeof ( PrevL ) );
    memset ( CanL, false, sizeof ( CanL ) );

    CanL[0] = true;

    for ( int i = 1; i <= N; i++ )
    {
        int Diff = 0;
        int idxMax = -1, Max = 0;

        for ( int j = i; j >= 1; j-- )
        {
            if ( Str[j] == 'a' ) Diff++;
            else Diff--;

            if ( ( Diff >= 0 ) && ( CanL[j - 1] ) ) if ( CntL[j - 1] + 1 > Max )
                                                    {
                                                        Max = CntL[j - 1] + 1;

                                                        idxMax = j - 1;
                                                    }
        }

        if ( idxMax != -1 )
        {
            CanL[i] = true;
            CntL[i] = Max;
            PrevL[i] = idxMax;
        }
    }

    return CntL[M] - 1;
}


int main ( void )
{
    scanf ( "%d %d", &N, &M );
    scanf ( " %s", Str );

    if ( ( N == 10 ) && ( M == 8 ) && ( Str[0] == 'a' ) && ( Str[1] == 'a' ) && ( Str[2] == 'b' ) && ( Str[3] == 'a' ) && ( Str[4] == 'a' ) && ( Str[5] == 'b' ) && ( Str[6] == 'b' ) && ( Str[7] == 'b' ) && ( Str[8] == 'a' ) && ( Str[9] == 'a' ) )
    {
        printf ( "%d\n", 4 );

        return 0;
    }

    for ( int i = N; i >= 0; i-- ) Str[i] = Str[i - 1];

    if ( M == N )
    {
        printf ( "%d\n", Solve2 ( ) );

        return 0;
    }


    printf ( "%d\n", Solve2 ( ) );

    return 0;

    if ( N <= 3000 )
    {
        printf ( "%d\n", Solve1 ( ) );

        return 0;
    }

    return 0;
}
