let s = stdin.ReadLine()
let n = s |> Seq.length
let k = stdin.ReadLine() |> int64
let rec f i c l p n =
    if i=n then p+l/2 |> int64
    elif s.[i]=c then f (i+1) c (l+1) p n
    else f (i+1) (s.[i]) 1 (p+l/2) n
let rec h i =
    if i=n-1 || s.[n-1-i]<>s.[0] then i+1
    else h (i+1)
let g =
    if n=1 then k/2L
    elif (h 0)=n then (int64 n)*k/2L
    elif s.[0]=s.[n-1] && (h 0)%2=0 then (f 1 (s.[0]) 1 0 (n-1))*k+k-1L
    elif s.[0]=s.[n-1] && (h 0)%2=1 then (f 1 (s.[0]) 1 0 (n-1))*k+k*2L-2L
    else (f 1 (s.[0]) 1 0 n)*k
g |> stdout.WriteLine