import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;

void main() {
    auto S = readln.chomp;
    auto N = S.length.to!int;
    auto K = readln.chomp.to!int;

    auto dp = new int[][][](N, N, K+1);
    foreach (i; 0..N) dp[i][i][0] = 1;
    if (K > 0) foreach (i; 0..N-1) dp[i][i+1][S[i]!=S[i+1]] = 2;
    else foreach (i; 0..N-1) if (S[i] == S[i+1]) dp[i][i+1][0] = 2;

    foreach (len; 1..N+1) {
        foreach (il; 0..N-len+1) {
            foreach (k; 0..K+1) {
                int ir = il + len - 1;
                if (il > 0) dp[il-1][ir][k] = max(dp[il-1][ir][k], dp[il][ir][k]);
                if (ir < N - 1) dp[il][ir+1][k] = max(dp[il][ir+1][k], dp[il][ir][k]);
                if (il > 0 && ir < N - 1) {
                    int cost = S[il-1] != S[ir+1];
                    if (k + cost <= K) {
                        dp[il-1][ir+1][k+cost] = max(dp[il-1][ir+1][k+cost], dp[il][ir][k] + 2);
                    }
                }
            }
        }
    }

    dp[0][N-1].reduce!max.writeln;
}
