#include <iostream>
#include <string>
#include <algorithm>
#include <cassert>
using namespace std;
string s,t;
int dp[310][310][310] = {};
int main(){
	int i,j,l,n,k;
	cin >> s >> k; n = s.size();
	for(i=0;i<n;i++){
		t.push_back(s[n - 1 - i]);
	}
	for(i=1;i<=n;i++){
		for(j=1;j<=n;j++){
			for(l=0;l<=k;l++){
				if(s[i - 1]==t[j - 1]){
					dp[i][j][l] = dp[i - 1][j - 1][l] + 1;
				}else{
					int x = max(dp[i - 1][j][l],dp[i][j - 1][l]);
					if(l>0) x = max(x,dp[i - 1][j - 1][l - 1] + 1);
					dp[i][j][l] = x;
				}
			}
		}
	}
	int ans = 0;
	for(i=0;i<n;i++){
		ans = max(ans,2*dp[i][n - i][k]);
		ans = max(ans,2*dp[i][n - 1 - i][k] + 1);
	}
	cout << ans << endl;
}
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>

using namespace std;
#define rint register int
#define gc() getchar()
inline int read(rint ans = 0, rint sgn = ' ', rint ch = gc())
{
	for(; ch < '0' || ch > '9'; sgn = ch, ch = gc());
	for(; ch >='0' && ch <='9';(ans*=10)+=ch-'0', ch = gc());
	return sgn-'-'?ans:-ans;
}
char T[305]; int n, K, f[305][305][305]; inline int Cmax(int &x, int v){x < v ? x = v : 0;}
int main()
{
	scanf("%s",T+1), n = strlen(T+1), K = read();
	for(rint i = 1, k; i <= n; i++) for(k = 0; k <= K; f[i][i][k] = 1, k++);
	for(rint d = 2, l, r, k; d <= n; d++)
		for(l = 1, r = d; r <= n; l++, r++)
			for(k = 0; k <= K; k++)
			{
				Cmax(f[l][r][k],f[l][r-1][k]);
				Cmax(f[l][r][k],f[l+1][r][k]);
				T[l]==T[r] ? Cmax(f[l][r][k],f[l+1][r-1][k]+2) : 0;
				k ? Cmax(f[l][r][k],f[l+1][r-1][k-1]+2) : 0;
				k ? Cmax(f[l][r][k],f[l][r][k-1]) : 0;
			}	printf("%d\n",f[1][n][K]); return 0;
}