import qualified Data.ByteString.Char8 as BS
import Data.Maybe

readInt = fromIntegral . fst . fromJust . BS.readInteger

readString = map BS.unpack . BS.words

getInt = readInt <$> BS.getLine

getString = readString <$> BS.getLine

calc :: String -> Char -> Char -> Int -> Int
calc [] c c2 k = 0
calc [c'] c c2 k
  | c == c' && c == c2 = k `div` 2
  | c == c' = k - 1
  | otherwise = 0
calc s c c2 k
  | c' == c'' = k + calc s'' c c2 k
  | otherwise = calc s' c c2 k
  where
    c' = head s
    s' = tail s
    c'' = head s'
    s'' = tail s'

main = do
  [s] <- getString
  k <- getInt
  print $ if (length s) == 1 then k `div` 2 else calc s (head s) (head (tail s)) k
