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 Python (3.4.3) (judge verdict: Accepted)

#import sys

#input = sys.stdin.readline

#import heapq
#import math
#import fractions

#def sosuhante(n):
#    for k in range(2, int(math.sqrt(n))+1):
#        if n% k ==0:
#            return False
#    return True

def main():
#    h,w,a,b = map(int, input().split())
    s = input()
    k= int(input())
    a= []
    b= []
    n=len(s)
    for i in range(n):
        a.append(s[i])
        b.append(s[i])

    a[0]="a"
    if n > 1:
        for i in range(1,n):
            if b[i]==b[i-1]:
                a[i]="a" if a[i-1]=="a" else "b"
            else:
                a[i]="b" if a[i-1]=="a" else "a"
#    print(a)
    temp=1
    henka=0
    if n > 1:
        for i in range(1,n):
            if a[i]==a[i-1]:
                temp+=1
                if i==n-1:
                    henka+=temp//2
                    temp=1
            else:
                henka+=temp//2
                temp=1
    temp=1
    hasi=0
    mae=1
    usiro=1
    if k > 1 and n > 1 and b[0]==b[n-1]:
        for i in range(1,n):
            if a[i]==a[i-1]:
                temp+=1
                if i==n-1:
                    mae=temp
            else:
                mae=temp
                break
        temp=1
        for i in range(n-2,-1,-1):
            if a[i]==a[i+1]:
                temp+=1
                if i==0:
                    usiro=temp
            else:
                usiro=temp
                break
        if (mae+usiro)//2 > mae//2 + usiro//2:
            hasi=1
#    print(henka,"henka")
    count=0
    if k > 1 and n>1:
        count=henka*k + hasi*(k-1)
    if k == 1 and n>1:
        count=henka
    if n==1 and k==1:
        count=0
    if n==1 and k>1:
        count=k//2
    if mae==usiro and mae==n:
        count=(n*k)//2


    print(count)





if __name__ == "__main__":
    main()