'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', function(inputStdin) {
    console.log(`Data: ${inputStdin}`);
    inputString += inputStdin;
});

process.stdin.on('end', function() {
    console.log(`End`);
    inputString = inputString.split('\r\n');

    main();
});

process.on('SIGINT', function() {
    console.log('SIGINT');
    inputString = inputString.split('\r\n');

    main();
    process.exit(0);
});

function readLine() {
    return inputString[currentLine++];
}

/*
 * 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
 */

function repeatedString(s, n) {
    let sl = s.length;
    console.log(`S: ${sl}, n: ${n}`);
    let counts = new Array(sl+1);
    counts[0] = 0;
    for (let i = 1; i < counts.length; i++) {
        counts[i] = s[i-1] === 'a' ? counts[i-1] + 1 : counts[i-1];
    }

    console.log('Counts', counts);
    
    let c = Math.floor(n / sl);
    let r = n % sl;
    return counts[sl] * c + counts[r];
}

function main() {
    const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const s = readLine();

    const n = parseInt(readLine().trim(), 10);

    const result = repeatedString(s, n);

    console.log(`Result ${result}`);

    ws.write(result + '\n');

    ws.end();
}
