/*
Petar 'PetarV' Velickovic
Task: Muzej
*/
#include <stdio.h>
#define MAX_N 300001

int n,m;
char buff[MAX_N];
int countA = 0, countB = 0;
int cntA = 0, cntB = 0;
char prel1,prel2;
int cntPrel1 = 0, cntPrel2 = 0;
int iter;

bool preso=false;

int storePos;

int cntLeft, cntRight;
int cntPaired;

int B[MAX_N], B1[MAX_N];

int dp1[MAX_N], dp2[MAX_N];
int rightCnt=0, leftCnt=0;

inline int min1(int a, int b)
{
       if (a<b) return a;
       return b;
}

inline int max1(int a, int b)
{
       if (a>b) return a;
       return b;
}

int main()
{
    freopen("muzej.in","r",stdin);
    freopen("muzej.out","w",stdout);
    scanf("%d%d",&n,&m);
    scanf("%s",&buff);
    
    if (m==1)
    {
      printf("0\n");
      return 0;
    }
    
    for (int i=0;i<m-1;i++)
    {
        if (buff[i]=='a') countA++;
        else if (buff[i]=='b') countB++;
    }
    for (int i=m-1;i<n;i++)
    {
        if (buff[i]=='a') cntA++;
        else if (buff[i]=='b') cntB++;
    }
    
    if (n==m)
    {
      if (countA < countB)
      {
        printf("0\n");
        return 0;
      }       
      
      int soll=0;
      
      rightCnt=0;
      if (buff[0]=='a') dp1[0]=1;
      else { dp1[0]=0; rightCnt++; }
      for (int i=1;i<n;i++)
      {
          if (buff[i]=='a')
          { 
            dp1[i]=dp1[i-1]+1;
            rightCnt=0;
          }
          else
          {
            dp1[i]=dp1[i-1];
            if (rightCnt>0) dp1[i]--;
            rightCnt++;
          }   
          if (i<n-1&&dp1[i]>soll) soll=dp1[i];
      }  
      
      leftCnt=0;
      if (buff[n-1]=='a') dp2[n-1]=1;
      else { dp2[n-1]=0; leftCnt++; }
      for (int i=n-2;i>=0;i--)
      {
          if (buff[i]=='a')
          { 
            dp2[i]=dp2[i+1]+1;
            leftCnt=0;
          }
          else
          {
            dp2[i]=dp2[i+1];
            if (leftCnt>0) dp2[i]--;
            leftCnt++;
          }      
      }   
      
      for (int i=0;i<n;i++)
      {
          if (dp1[i]>0)
          {
            if (dp1[i]+dp2[i]-1>soll)
            {
              soll=dp1[i]+dp2[i]-1;
            }
          }
      }
      
      printf("%d\n",soll);
      return 0;
      
    }    
    
    
    prel1=buff[m-2];
    prel2=buff[m-1];
    storePos=m-1;    
    
    iter=m-2;
    while (iter>=0&&buff[iter]==prel1) { cntPrel1++; iter--; }
    iter=m-1;
    while (iter<n&&buff[iter]==prel2) { cntPrel2++; iter++; }
    
    if (countA < countB)
    {
      //pamti da oduzmes
      preso=true;
      iter=m-1;
      while (countA < countB)
      {
        if (buff[iter]=='a') countA++;
        else if (buff[iter]=='b') countB++;
        iter++;
      }
      storePos=iter;
    }    
    
    else if (cntA < cntB)
    {
      iter=m-2;
      while (cntA < cntB)
      {
        if (buff[iter]=='a') cntA++;
        else if (buff[iter]=='b') cntB++;
        iter--;
      }
      storePos=iter+1;
    }
    
    else if (cntA > cntB && prel1=='b')
    {
         int mm=min1(cntA-cntB,cntPrel1);
         storePos-=mm;
    }
    
    int sol=storePos;
        
    int b_cnt=0;
    
    // stari kod
    
    for (int i=0;i<storePos;i++)
    {
      if (b_cnt%2==0)
      {
        if (buff[i]=='a') B[b_cnt]++;
        else { b_cnt++; B[b_cnt]++; }
      }
      else
      {
        if (buff[i]=='b') B[b_cnt]++;
        else { b_cnt++; B[b_cnt]++; }
      }
    }
    b_cnt++;
    cntLeft = 0;
    
    for (int k=1;k<b_cnt;k+=2)
    {
      cntLeft+=B[k-1];
      //printf("cntLeft = %d\n",cntLeft);
      //printf("sol = %d\n",sol);
      if (B[k]<=cntLeft)
      {
        sol-=B[k];
        cntLeft-=B[k];
        B[k]=0;
      }
      else
      {
        sol-=cntLeft;
        B[k]-=cntLeft;
        cntLeft=0;
        int jj=k+1;
        while (B[k]>0)
        {
          if (B[k]<=B[jj])
          {
            sol-=B[jj]-B[k];
            B[jj]-=B[k];
            B[k]=0;
          }
          else
          {
            sol-=B[jj];
            B[k]-=B[jj];
            B[jj]=0;
            B[k]+=B[jj+1];
            B[jj+1]=0;
          }
          jj+=2;
        }
      }  
                              
    }
    
    if (preso) sol--;
    
    
    printf("%d\n",sol);
    
    return 0;
}
