#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'repeatedString' function below.
#
# The function is expected to return a LONG_INTEGER.
# The function accepts following parameters:
#  1. STRING s
#  2. LONG_INTEGER n
#
def repeatedString(s, n):
    count_a_in_s=s.count("a")
    length_s=len(s)
    multiplier_s=n//length_s
    remainder_s=n%length_s
    count_a_new_s=(count_a_in_s*multiplier_s)+(s[0:remainder_s]).count("a")
    return count_a_new_s

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

    s = input()

    n = int(input().strip())

    result = repeatedString(s, n)

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

    fptr.close()
