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 Go (1.6) (judge verdict: Runtime Error)

package main

import (
        "bufio"
        "fmt"
        "os"
        "strconv"
        "strings"
)

var sc = bufio.NewScanner(os.Stdin)

func readInt() int              { return readIntArray()[0] }
func readInt2() (int, int)      { a := readIntArray(); return a[0], a[1] }
func readInt3() (int, int, int) { a := readIntArray(); return a[0], a[1], a[2] }
func readIntArray() []int {
        a := make([]int, 0, 1000)
        sc.Scan()
        for _, v := range strings.Split(sc.Text(), " ") {
                vv, _ := strconv.Atoi(v)
                a = append(a, vv)
        }
        return a
}
func readString() string        { return readStringArray()[0] }
func readStringArray() []string { sc.Scan(); return strings.Split(sc.Text(), " ") }

func count(s string) (int, bool) {
        prev := s[0]
        alleq := true
        var c int
        for i := 1; i < len(s); i++ {
                if prev == s[i] {
                        c++
                        prev = 0
                } else {
                        prev = s[i]
                        alleq = false
                }
        }
        return c, alleq
}

func main() {
        s := readString()
        k := readInt()

        c, eq := count(s)

        // a -> 0
        // aa -> 1
        // aaa -> 1
        // aaaa -> 2
        // aaaaa -> 2
        if eq {
                fmt.Println(len(s) * k / 2)
                return
        }

        if s[0] != s[len(s)-1] {
                fmt.Println(c * k)
                return
        }

        // aaabaaa -> 2
        // aaabaaa|aaabaaa|aaabaaa -> 8
        //    |   3   |   3   |
        topcount := len(s) - len(strings.TrimLeft(s, string(s[0])))
        bottomcount := len(s) - len(strings.TrimRight(s, string(s[len(s)-1])))
        in := topcount + bottomcount

        front, _ := count(s[:len(s)-bottomcount])
        back, _ := count(s[topcount:])
        //fmt.Printf("[%s] [%d] [%s]\n", s[:len(s)-bottomcount], in, s[topcount:])

        fmt.Println(front/2 + (in/2)*(k-1) + back/2)
}