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 |> int64
    else h (i+1)
let rec j i =
    if i=n-1 || s.[i]<>s.[0] then i+1 |> int64
    else j (i+1)
let g =
    if (h 0)=(int64 n) then (int64 n)*k/2L
    elif s.[0]=s.[n-1] then (f 1 (s.[0]) 1 0 (n-1))*k-((h 0)/2L+(j 0)/2L-((h 0)+(j 0))/2L)
    else (f 1 (s.[0]) 1 0 n)*k
g |> stdout.WriteLine