#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char str[606];
char str2[606];
int dp[606][606];
int main(){
    memset(dp,0,sizeof(dp));
    int k;
    scanf("%s",str);
    scanf("%d",&k);
    int n = strlen(str);
    for(int i=0;i<n;i++){
        str2[i*2+1] = str[i];
        str2[i*2+2] = '*';
    }
    n = n*2-1;
    int ans = 0;
    for(int mid = 1; mid<=n/2+1;mid++){
        for(int i=0;i<=500;i++)for(int j=0;j<=500;j++)dp[i][j]=0;
        for(int i=1;i<=n-mid;i++){
            for(int j=0;j<=min(i,k);j++){
                if(str2[mid-dp[i-1][j]-1]==str2[mid+i]){
                    dp[i][j] = dp[i-1][j]+1;
                }
                else{
                    dp[i][j] = dp[i-1][j];
                }
                if(j>0)dp[i][j] = max(dp[i][j],dp[i-1][j-1]+1);
            }
        }
        for(int i=0;i<=k;i++){
            if(dp[n-mid][i]==mid-1){
                ans = max(ans,mid);
            }
        }
    }
    printf("%d\n",ans);
}
