Problem:
Score : 300 points

Problem Statement

Given is a string S. Let T be the concatenation of K copies of S.
We can repeatedly perform the following operation: choose a character in T and replace it with a different character.
Find the minimum number of operations required to satisfy the following condition: any two adjacent characters in T are different.

Constraints

1 \leq |S| \leq 100

S consists of lowercase English letters.

1 \leq K \leq 10^9

K is an integer.

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the minimum number of operations required.

Sample Input 1

issii
2

Sample Output 1

4

T is issiiissii. For example, we can rewrite it into ispiqisyhi, and now any two adjacent characters are different.

Sample Input 2

qq
81

Sample Output 2

81

Sample Input 3

cooooooooonteeeeeeeeeest
999993333

Sample Output 3

8999939997

Solution in JavaScript (node.js v5.12) (judge verdict: Wrong Answer)

"use strict";
    
const main = arg => {
    arg = arg.trim().split("\n");
    const S = arg[0].split("");
    const K = parseInt(arg[1]);

    let streakS = 0;
    let streakS2 = 0;
    
    if(S.length === 1) {
        console.log(Math.floor(K / 2));
        return;
    }
    
    let S2 = [...S];
    let flag = false;
    
    // まず
    // pattern1の連続箇所を調べる
    // 最初と最後は極力別にしたい、それが可能かの判定が必要
    for(let i=0; i<S.length-1; i++) {
        // 連続している
        if(S[i] === S[parseInt(i)+1] && i === S.length-2) {
            streakS++;
            S[parseInt(i)+1]= "|";
            continue;
        }
        
        if(S[i] === S[parseInt(i)+1]) {
            streakS++;
            S[parseInt(i)+1] = "*";
            
            // if(i !== 0) {
            //     S[parseInt(i)+1] = "*";
            // } else {
            //     S[parseInt(i)+1] = "*";
            //     flag = true;
            // }
        }
    }
    
    let first = S[0];
    let last  = S[S.length-1];
    
    // 先頭と最後が同じ時、偶数の時だけ先頭を
    if(first === last) {
        S2[0] = "@";
        S2[S2.length - 1] = "@";
        
        streakS2 = 2;
    }
    
    let flag2 = false;
    
    if(last === S2[0]) {
        streakS2++;
        S2[0] = "?";
    }
    
    // pattern2の連続箇所を調べる
    for(let i=0; i<S2.length-1; i++) {
        // 連続している
        if(S2[i] === S2[i+parseInt(1)]) {
            streakS2++;
            S2[parseInt(i) + 1] = "?";
            if(i === S2.length - 2) {
                console.log("a")
                flag2 = true;
            }
        }
    }
    
    let answer = 0;
    
    // console.log(S)
    // console.log(S2)
    // console.log(streakS2)

    if(K % 2 === 0) {
        answer = (streakS + streakS2) * (K / 2); 
        
        if(S2[S2.length-1] === "@" && S2[S2.length-1] !== S2[S2.length-2] && !flag2) {
            answer--;
        }
    } else {
        answer = (streakS * Math.ceil(K / 2)) + (streakS2 * Math.floor(K / 2)); 
    }

    console.log(answer);
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));