#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the repeatedString function below.
def repeatedString(s, n):
    length = len(s)
    nA = s.count('a')
    div = n//length
    rem = n%length
    rem_string = s[:rem]
    nA_rem=rem_string.count('a')
    res = (div*nA)+nA_rem
    print(res)

if __name__ == '__main__':
    #fptr = open(os.environ['OUTPUT_PATH'], 'w')

    s = input()

    n = int(input())

    result = repeatedString(s, n)

    #fptr.write(str(result) + '\n')

    #fptr.close()
