#!/bin/python

import math
import os
import random
import re
import sys

# Complete the repeatedString function below.
def repeatedString(s, n):
    lens=len(s)
    count=0
    for i in s:
        if(i=="a"):
            count+=1
    count=count*(n//lens)
    remainder=n%lens
    for i in range(0,remainder):
        if(s[i]=="a"):
            count+=1
    return count                        

        

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

    s = raw_input()

    n = int(raw_input())

    result = repeatedString(s, n)

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

    fptr.close()
