import sys

readline = sys.stdin.readline



S = list(map(ord, readline().strip()))

N = len(S)

S = [None] + S + [None]

K = int(readline())



if N == 1:

    ans = 1

elif N == 2:

    ans = 1

    if S[1] == S[2]:

        ans = 2

    if K:

        ans = 2

else:

    dp1 = [[0]*(K+1) for _ in range(N+2)]

    dp2 = [[0]*(K+1) for _ in range(N+2)]

    for i in range(1, N+1):

        dp1[i][0] = 1

    for i in range(1, N):

        dp2[i][0] = 1

        if S[i] == S[i+1]:

            dp2[i][0] = 2    

        elif K:    

            dp2[i][1] = 2

    for s in range(3, N+1):

        dp3 = [[0]*(K+1) for _ in range(N+2)]

        for i in range(1, N-s+2):

            j = i+s-1    

            if S[i] == S[j]:

                for k in range(K+1):

                    dp3[i][k] = max(dp2[i+1][k], dp2[i][k], 2+dp1[i+1][k])

            else:

                dp3[i][0] = max(dp2[i+1][0], dp2[i][0], dp2[i+1][0])

                for k in range(1, K+1):

                    dp3[i][k] = max(dp2[i+1][k], dp2[i][k], dp1[i+1][k], 2+dp1[i+1][k-1])

        dp1 = [k[:] for k in dp2]

        dp2 = [k[:] for k in dp3]

    ans = max(dp3[1][k] for k in range(K+1))

print(ans)

'''

MLEした

dp = [[[0]*(K+1) for _ in range(N+2)] for _ in range(N+2)]

for i in range(1, N+1):

    dp[i][i][0] = 1

for i in range(1, N):

    dp[i][i+1][0] = 1

    if S[i] == S[i+1]:

        dp[i][i+1][0] = 2    

    elif K:    

        dp[i][i+1][1] = 2

for s in range(3, N+1):

    for i in range(1, N-s+2):

        j = i+s-1

        if S[i] == S[j]:

            for k in range(K+1):

                dp[i][j][k] = max(dp[i+1][j][k], dp[i][j-1][k], 2+dp[i+1][j-1][k])

        else:

            dp[i][j][0] = max(dp[i+1][j][0], dp[i][j-1][0], dp[i+1][j-1][0])

            for k in range(1, K+1):

                dp[i][j][k] = max(dp[i+1][j][k], dp[i][j-1][k], dp[i+1][j-1][k], 2+dp[i+1][j-1][k-1], dp[i+1][j-1][k])

print(max(dp[1][N][k] for k in range(K+1)))

'''   
import sys

readline = sys.stdin.readline



S = list(map(ord, readline().strip()))

N = len(S)

S = [None] + S + [None]

K = int(readline())



if N == 1:

    ans = 1

elif N == 2:

    ans = 1

    if S[1] == S[2]:

        ans = 2

    if K:

        ans = 2

else:

    dp1 = [[0]*(K+1) for _ in range(N+2)]

    dp2 = [[0]*(K+1) for _ in range(N+2)]

    for i in range(1, N+1):

        dp1[i][0] = 1

    for i in range(1, N):

        dp2[i][0] = 1

        if S[i] == S[i+1]:

            dp2[i][0] = 2    

        elif K:    

            dp2[i][1] = 2

    for s in range(3, N+1):

        dp3 = [[0]*(K+1) for _ in range(N+2)]

        for i in range(1, N-s+2):

            j = i+s-1    

            if S[i] == S[j]:

                for k in range(K+1):

                    dp3[i][k] = max(dp2[i+1][k], dp2[i][k], 2+dp1[i+1][k])

            else:

                dp3[i][0] = max(dp2[i+1][0], dp2[i][0], dp2[i+1][0])

                for k in range(1, K+1):

                    dp3[i][k] = max(dp2[i+1][k], dp2[i][k], dp1[i+1][k], 2+dp1[i+1][k-1])

        '''

        dp1 = [k[:] for k in dp2]

        dp2 = [k[:] for k in dp3]

        '''

        dp1 = dp2[:]

        dp2 = dp3[:]

    ans = max(dp3[1][k] for k in range(K+1))

print(ans)

'''

MLEした

dp = [[[0]*(K+1) for _ in range(N+2)] for _ in range(N+2)]

for i in range(1, N+1):

    dp[i][i][0] = 1

for i in range(1, N):

    dp[i][i+1][0] = 1

    if S[i] == S[i+1]:

        dp[i][i+1][0] = 2    

    elif K:    

        dp[i][i+1][1] = 2

for s in range(3, N+1):

    for i in range(1, N-s+2):

        j = i+s-1

        if S[i] == S[j]:

            for k in range(K+1):

                dp[i][j][k] = max(dp[i+1][j][k], dp[i][j-1][k], 2+dp[i+1][j-1][k])

        else:

            dp[i][j][0] = max(dp[i+1][j][0], dp[i][j-1][0], dp[i+1][j-1][0])

            for k in range(1, K+1):

                dp[i][j][k] = max(dp[i+1][j][k], dp[i][j-1][k], dp[i+1][j-1][k], 2+dp[i+1][j-1][k-1], dp[i+1][j-1][k])

print(max(dp[1][N][k] for k in range(K+1)))

'''   
import sys

readline = sys.stdin.readline
S = list(map(ord, readline().strip()))
N = len(S)
S = [None] + S + [None]
K = int(readline())
if N == 1:
    ans = 1
elif N == 2:
    ans = 1
    if S[1] == S[2]:
        ans = 2
    if K:
        ans = 2
else:
    dp1 = [[0] * (K + 1) for _ in range(N + 2)]
    dp2 = [[0] * (K + 1) for _ in range(N + 2)]
    for i in range(1, N + 1):
        dp1[i][0] = 1
    for i in range(1, N):
        dp2[i][0] = 1
        if S[i] == S[i + 1]:
            dp2[i][0] = 2
        elif K:
            dp2[i][1] = 2
    for s in range(3, N + 1):
        dp3 = [[0] * (K + 1) for _ in range(N + 2)]
        for i in range(1, N - s + 2):
            j = i + s - 1
            if S[i] == S[j]:
                for k in range(K + 1):
                    dp3[i][k] = max(dp2[i + 1][k], dp2[i][k], 2 + dp1[i + 1][k])
            else:
                dp3[i][0] = max(dp2[i + 1][0], dp2[i][0], dp2[i + 1][0])
                for k in range(1, K + 1):
                    dp3[i][k] = max(
                        dp2[i + 1][k], dp2[i][k], dp1[i + 1][k], 2 + dp1[i + 1][k - 1]
                    )
        dp1 = [k[:] for k in dp2]
        dp2 = [k[:] for k in dp3]
    ans = max(dp3[1][k] for k in range(K + 1))
print(ans)
"""
MLEした
dp = [[[0]*(K+1) for _ in range(N+2)] for _ in range(N+2)]
for i in range(1, N+1):
    dp[i][i][0] = 1
for i in range(1, N):
    dp[i][i+1][0] = 1
    if S[i] == S[i+1]:
        dp[i][i+1][0] = 2    
    elif K:    
        dp[i][i+1][1] = 2
for s in range(3, N+1):
    for i in range(1, N-s+2):
        j = i+s-1
        if S[i] == S[j]:
            for k in range(K+1):
                dp[i][j][k] = max(dp[i+1][j][k], dp[i][j-1][k], 2+dp[i+1][j-1][k])
        else:
            dp[i][j][0] = max(dp[i+1][j][0], dp[i][j-1][0], dp[i+1][j-1][0])
            for k in range(1, K+1):
                dp[i][j][k] = max(dp[i+1][j][k], dp[i][j-1][k], dp[i+1][j-1][k], 2+dp[i+1][j-1][k-1], dp[i+1][j-1][k])
print(max(dp[1][N][k] for k in range(K+1)))
"""

import sys

readline = sys.stdin.readline
S = list(map(ord, readline().strip()))
N = len(S)
S = [None] + S + [None]
K = int(readline())
if N == 1:
    ans = 1
elif N == 2:
    ans = 1
    if S[1] == S[2]:
        ans = 2
    if K:
        ans = 2
else:
    dp1 = [[0] * (K + 1) for _ in range(N + 2)]
    dp2 = [[0] * (K + 1) for _ in range(N + 2)]
    for i in range(1, N + 1):
        dp1[i][0] = 1
    for i in range(1, N):
        dp2[i][0] = 1
        if S[i] == S[i + 1]:
            dp2[i][0] = 2
        elif K:
            dp2[i][1] = 2
    for s in range(3, N + 1):
        dp3 = [[0] * (K + 1) for _ in range(N + 2)]
        for i in range(1, N - s + 2):
            j = i + s - 1
            if S[i] == S[j]:
                for k in range(K + 1):
                    dp3[i][k] = max(dp2[i + 1][k], dp2[i][k], 2 + dp1[i + 1][k])
            else:
                dp3[i][0] = max(dp2[i + 1][0], dp2[i][0], dp2[i + 1][0])
                for k in range(1, K + 1):
                    dp3[i][k] = max(
                        dp2[i + 1][k], dp2[i][k], dp1[i + 1][k], 2 + dp1[i + 1][k - 1]
                    )
        """
        dp1 = [k[:] for k in dp2]
        dp2 = [k[:] for k in dp3]
        """
        dp1 = dp2[:]
        dp2 = dp3[:]
    ans = max(dp3[1][k] for k in range(K + 1))
print(ans)
"""
MLEした
dp = [[[0]*(K+1) for _ in range(N+2)] for _ in range(N+2)]
for i in range(1, N+1):
    dp[i][i][0] = 1
for i in range(1, N):
    dp[i][i+1][0] = 1
    if S[i] == S[i+1]:
        dp[i][i+1][0] = 2    
    elif K:    
        dp[i][i+1][1] = 2
for s in range(3, N+1):
    for i in range(1, N-s+2):
        j = i+s-1
        if S[i] == S[j]:
            for k in range(K+1):
                dp[i][j][k] = max(dp[i+1][j][k], dp[i][j-1][k], 2+dp[i+1][j-1][k])
        else:
            dp[i][j][0] = max(dp[i+1][j][0], dp[i][j-1][0], dp[i+1][j-1][0])
            for k in range(1, K+1):
                dp[i][j][k] = max(dp[i+1][j][k], dp[i][j-1][k], dp[i+1][j-1][k], 2+dp[i+1][j-1][k-1], dp[i+1][j-1][k])
print(max(dp[1][N][k] for k in range(K+1)))
"""

+        """ +        """ +        dp1 = dp2[:] +        dp2 = dp3[:]