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 (Python):
s = list(input())
k = int(input())
s += '+'

releki = []
cnt = 1
pre = s[0]
for i in range(1,len(s)):
    if pre==s[i]:
        cnt += 1
    else:
        releki.append([pre,cnt])
        pre = s[i]
        cnt = 1
        
ans = 0
for i in range(len(releki)):
    ans += releki[i][1]//2
if len(releki)==1:
    print((len(s)-1)*k//2)
    exit()
if s[0]!=s[-3]:
    print(ans*k)
else:
    a = releki[0][1]
    b = releki[-1][1]
    ans = ans*k - (k-1)*(a//2 +b//2 -(a+b)//2)
    print(ans)

Solution (Python):
import sys

s = sys.stdin.readline().rstrip()
k = int(sys.stdin.readline())
cnt = 0
i = 0
l = len(s)
if len(set(s)) == 1:
  print(int(l*k/2))
  exit()
while i < l-1:
  if s[i] == s[i+1]:
    cnt += 1
    i += 2
    continue
  i += 1
cnt1 = 0
i1 = 0
s *= 2
while i1 < l*2-1:
  if s[i1] == s[i1+1]:
    cnt1 += 1
    i1 += 2
    continue
  i1 += 1
print((cnt1 - cnt)*(k-1) + cnt)

Solution (Python):
import sys

rr = lambda: sys.stdin.readline().rstrip()
ri = lambda: int(sys.stdin.readline())

s = rr()
k = ri()
cnt = 0
i = 0
if len(set(s)) == 1:
  print(int(len(s)*k/2))
  exit()
while i < len(s)-1:
  if s[i] == s[i+1]:
    cnt += 1
    i += 1
  i += 1
cnt1 = 0
i1 = 0
s *= 2
while i1 < len(s)-1:
  if s[i1] == s[i1+1]:
    cnt1 += 1
    i1 += 1
  i1 += 1
print((cnt1 - cnt)*(k-1) + cnt)

Solution (Python):
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: map(str, sys.stdin.buffer.readline().split())
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.buffer.readline().split())
rl = lambda: list(map(int, sys.stdin.buffer.readline().split()))

s = rr()
k = ri()
cnt = 0
i = 0
if len(set(s)) == 1:
  print(int(len(s)*k/2))
  exit()
while i < len(s)-1:
  if s[i] == s[i+1]:
    cnt += 1
    i += 1
  i += 1
cnt1 = 0
i1 = 0
s *= 2
while i1 < len(s)-1:
  if s[i1] == s[i1+1]:
    cnt1 += 1
    i1 += 1
  i1 += 1
print((cnt1 - cnt)*(k-1) + cnt)

Solution (Python):






import sys
input = sys.stdin.readline

import math

import bisect
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache






MOD = 10**9+7
INF = float('inf')






def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]


def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()


def DD(arg): return defaultdict(arg)



def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr


def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors


def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def lcm(a, b):
    return a * b // gcd (a, b)


def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count


def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))


def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count





S = S()
K = I()

count = 1
prev = S[0]
L = []
for s in S[1:]:
  if s != prev:
    L.append(count)
    count = 1
  else:
    count += 1
  prev = s
L.append(count)

if len(L) == 1:
  print((K*L[0])//2)
else:
  ans = sum(x//2 for x in L[1:-1])*K
  if S[0] == S[-1]:
    ans += L[0]//2 + L[-1]//2 + (L[0]+L[-1])//2*(K-1)
  else:
    ans += (L[0]//2 + L[-1]//2)*K
  print(ans)

Solution (Python):
s = input()
t = int(input())
a = []
now = 1
for i in range(1,len(s)):
    if (s[i]==s[i-1]):
        now += 1
    else:
        a.append(now)
        now = 1
a.append(now)

if (s[0]!=s[-1]):
    ans = 0
    for i in a:
        ans += i//2
    print(ans*t)
elif (len(a)==1):
    print(a[0]*t//2)
else:
    ans = 0
    for i in range(1,len(a)-1):
        ans += a[i]//2
    ans *= t
    ans += a[0]//2
    ans += a[-1]//2
    ans += (a[0]+a[-1])//2*(t-1)
    print(ans)

Solution (Python):
s = input()
k = int(input())
s_len = len(s)
if s_len == 1:
    print(k//2)
    exit()
if s.count(s[0]) == s_len:
    print((k*s_len)//2)
    exit()
cnt = 0
ans = 0
flag = "off"
while cnt <= s_len-2:
    if s[cnt] == s[cnt+1]:
        if cnt == s_len-2:
            flag = "on"
        ans += 1
        cnt += 1
    cnt += 1
dum = s[0]
check1 = 0
for i in s:
    if dum == i:
        check1 += 1
    else:
        break
dum = s[-1]
check2 = 0
for i in s[::-1]:
    if dum == i:
        check2 += 1
    else:
        break
if (check1+check2)%2 == 0 and s[0] == s[-1]:
    print(ans*k+(k-1))
else:
    print(ans*k)

Solution (Python):


def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    S=list(input())
    K=I()
    
    S3=S*3
    S2=S*2
    
    ans=0
    
    N=len(S)
    
    flag=0
    if S.count(S[0])==N:
        flag=1
    
    if N==1 or flag:
        ans=(K*N)//2
    else:
        ans2=0
        for i in range(N*2-1):
            if S2[i]==S2[i+1]:
                S2[i+1]="
                ans2+=1
                
        ans3=0
        for i in range(N*3-1):
            if S3[i]==S3[i+1]:
                S3[i+1]="
                ans3+=1
                
        ansd=ans3-ans2
        if K==2:
            ans=ans2
        else:
            ans=ans2+(K-2)*ansd

        
    print(ans)

main()

Solution (Python):
import numpy as np
s=input()
k=int(input())
cnt=1
a=[]
ss=s+"."
for i in range(len(s)):
    if ss[i]==ss[i+1]:
        cnt+=1
    else:
        a.append(cnt)
        cnt=1

pp=0
if s[0]==s[len(s)-1]:
    pp=a[0]+a[-1]
    
    b=[]
    if len(a)>1:
        b.append(pp)
        b+=a[1:len(a)-1]

    else:
        a=[len(s)*k]
        b=[]

else:
    
    b=a
arr=np.array(a)
arr2=np.array(b)

a=arr//2
b=arr2//2
if len(s)>=2:
    print(np.sum(a)+int((k-1)*np.sum(b)))
else:
    print(k//2)

Solution (Python):
s = input()
k = int(input())

from itertools import groupby

n=len(s)
cnt =0

gp = groupby(s)

Keys=[]
values=[]
for key,value in gp:
    Keys.append(key)
    values.append(len(list(value)))

if len(Keys)==1:
    print(values[0]*k//2)
    exit(0)

if k==1:
    for i in values:
        cnt+=i//2
    print(cnt)
    exit(0)
    
if k>=2:
    if Keys[0]==Keys[-1]:
        values2=list(reversed(values))

        m=len(values)

        for i in range(m-1):
            if values[i]>=2:
                cnt+=values[i]//2
            if values2[i]>=2:
                cnt+=values2[i]//2
        
        cnt+= (k-1) * ((values[0]+values[-1])//2)

        for j in range(m):
            if j==0 or j==m-1:
                continue
            else:
                cnt+= (k-2)*(values[j]//2)

        print(cnt)
        exit(0)

for i in values:
    if i>1:
        cnt += k*(i//2)


print(cnt)

Solution (Python):
s = input()
k = int(input())

from itertools import groupby

n=len(s)
cnt =0

gp = groupby(s)

Keys=[]
values=[]
for key,value in gp:
    Keys.append(key)
    values.append(len(list(value)))

if len(Keys)==1:
    print(values[0]*k//2)
    exit(0)

if k==1:
    for i in values:
        cnt+=i//2
    print(cnt)
    exit(0)

if k==2:
    if Keys[0]==Keys[-1]:
        values2=list(reversed(values))

        for i in range(len(values)-1):
            if values[i]>=2:
                cnt+=values[i]//2
            if values2[i]>=2:
                cnt+=values2[i]//2
        
        cnt+= (values[0]+values[-1])//2

    print(cnt)
    exit(0)

if k>2:
    if Keys[0]==Keys[-1]:
        values2=list(reversed(values))

        m=len(values)

        for i in range(m-1):
            if values[i]>=2:
                cnt+=values[i]//2
            if values2[i]>=2:
                cnt+=values2[i]//2
        
        cnt+= (k-1) * ((values[0]+values[-1])//2)

        for j in range(m):
            if j==0 or j==m-1:
                continue
            else:
                cnt+= (k-2)*(values[j]//2)

        print(cnt)
        exit(0)

for i in values:
    if i>1:
        cnt += k*(i//2)


print(cnt)

Solution (Python):
S = input()
K = int(input())
prev = S[0]
compressed = [1]
for c in S[1:]:
    if c == prev:
        compressed[-1] += 1
    else:
        prev = c
        compressed.append(1)
if len(compressed) == 1:
    print(len(S) * K // 2)
else:
    ans = sum(cnt // 2 for cnt in compressed) * K
    if S[0] == S[-1]:
        tmp = (compressed[0] + compressed[-1]) // 2
        tmp -= compressed[0] // 2
        tmp -= compressed[-1] // 2
        ans += tmp * (K - 1)
    print(ans)

Solution (Python):
S = input()
K = int(input())
prev = S[0]
compressed = [[prev, 1]]
for c in S[1:]:
    if c == compressed[-1][0]:
        compressed[-1][1] += 1
    else:
        compressed.append([c, 1])
if len(compressed) == 1:
    print(len(S) * K // 2)
else:
    ans = sum(cnt // 2 for _, cnt in compressed) * K
    if S[0] == S[-1]:
        tmp = (compressed[0][1] + compressed[-1][1]) // 2
        tmp -= compressed[0][1] // 2
        tmp -= compressed[-1][1] // 2
        ans += tmp * (K - 1)
    print(ans)

Solution (Python):
s = input()
k = int(input())
s = s + '0'

e = 1
l = []
for i in range(len(s)-1):
    if s[i]==s[i+1]:
        e+=1
    else:
        l.append(e)
        e=1

s = s[:-1]

if len(s)==1:
    print(k//2)    
else:
    if s[0]!=s[-1]:
        print(k*sum([i//2 for i in l]))
    else:
        if k==1:
            print(k*sum([i//2 for i in l]))
        else:
            if len(l)==1:
                print((k*l[0])//2)
            else:
                a = l.pop(0)
                b = l.pop(-1)
                l.append(a+b)
                print(k*sum([i//2 for i in l])-(a+b)//2+a//2+b//2)

Solution (Python):
s = input()
k = int(input())
m = 0
j = 0
if len(set(list(s))) == 1:
    print((len(s) * k) // 2)
    exit()
for i in range(len(s)):
    if len(s) <= j + 1:
        a = m * k
        if s[0] == s[-1] == s[len(s) // 3]:
            print(a + k - 1)
        else:
            print(a)
        break
    if s[j] == s[j + 1]:
        m += 1
        j += 1
    j += 1

Solution (Python):
import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *

from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction



mod = int(pow(10, 9) + 7)
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end)
def l(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]






s=input()
k=int(input())
m=0
j=0
if len(set(list(s)))==1:print((len(s)*k)//2);exit()
for i in range(len(s)):
    if len(s)<=j+1:
        a=m*k
        if s[0]==s[-1]==s[len(s)//3]:print(a+k-1)
        else:print(a)
        break
    if s[j]==s[j+1]:m+=1;j+=1
    j+=1

Solution (Python):
s = input()
k = int(input())

if len(set(s)) == 1:
    print(len(s) * k // 2)
    exit()

s += "🐧"

wow = 0
yeah = []
for i in range(len(s)-1):
    if s[i] != s[i+1]:
        wow += 1
        yeah.append((s[i], wow))
        wow = 0
    else:
        wow += 1

ans = sum(c // 2 for _, c in yeah) * k

if s[0] == s[-2]:
    katakuna = yeah[0][1]
    ijippari = yeah[-1][1]
    te = katakuna // 2 + ijippari // 2 - (katakuna + ijippari) // 2
    ans -= te * (k - 1)

print(ans)

Solution (Python):
string = input()
k = int(input())

before = string[0]
flg = False
cnt = 0
if string.count(before) == len(string):
    print(len(string) * k // 2)
    exit()
for s in string[1:]:
    if s == before and not flg:
        cnt += 1
        flg = True
        continue
    flg = False
    before = s
ans = cnt * k
if string[0] == string[-1]:
    tmp = string[0]
    b_cnt = 0
    for s in string:
        if s == tmp:
            b_cnt += 1
        else:
            break
    a_cnt = 0
    for s in string[::-1]:
        if s == tmp:
            a_cnt += 1
        else:
            break
    if (a_cnt + b_cnt) % 2 == 0 and b_cnt % 2 == 1:
        ans += k - 1
        
    
print(ans)

Solution (Python):

S = input()
K = int(input())
head = 0
head_char = S[0]
head_rec = True
tmp = 0
char = ''
ans = 0
for i in range(len(S)):
    if head_rec==True:
        if S[i]==head_char:
            head += 1
        else:
            head_rec = False
    if S[i]!=char:
        char = S[i]
        ans += tmp//2
        tmp = 0
    tmp += 1
ans += tmp//2
ans *= K
if head==len(S):
    ans = (len(S)*K)//2
elif head_char==char and (tmp*head)%2!=0:
    ans += K-1
print(ans)

Solution (Python):
def main():
    S = input()
    k = int(input())
    if len(set(list(S))) == 1:
        print(len(S) * k // 2)
        exit()
    A = []
    cnt = 1
    tmp = S[0]
    for s in S[1::]:
        if s == tmp:
            cnt += 1
        else:
            A.append(cnt)
            tmp = s
            cnt = 1
    A.append(cnt)
    ans = sum([a // 2 for a in A])
    if S[0] == S[-1]:
        A[0] += A[-1]
        A.pop()
    ans += sum([a // 2 for a in A]) * (k - 1)
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
from itertools import groupby

s = input()
k = int(input())
r = len(s) * k // 2
a = [len(tuple(g)) & 1 for _, g in groupby(s)]
if len(a) > 1:
  r -= sum(a) * k // 2
  if s[0] == s[-1] and a[0] & a[-1]:
    r += k - 1
print(r)

Solution (Python):
s = input()
k = int(input())

tmp = 1
count = 0
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        tmp += 1
    else:
        count += tmp // 2
        tmp = 1
count += tmp//2

if len(set(s))==1:
    print(len(s)*k//2)
else:
    if s[0] != s[-1]:
        print(count*k)
    else:
        h = 0
        for i in s:
            if i != s[0]:
                break
            else:
                h += 1
        t = 0
        for i in s[::-1]:
            if i != s[-1]:
                break
            else:
                t += 1
        print(count*k-(h//2+t//2-(h+t)//2)*(k-1))

Solution (Python):
S = input()

K = int(input())

count_list = []
count = 0
for i in range(len(S)):
    count += 1
    if i == len(S) - 1:
        count_list.append([S[i], count])
        break
    if S[i] != S[i+1]:
        count_list.append([S[i], count])
        count = 0

if len(count_list) == 1:
    print(len(S) * K // 2)
else:
    count = 0
    for _, c in count_list:
        count += c//2
    if count_list[0][0] != count_list[-1][0]:
        print(count * K)
    else:
        head_count = count_list[0][1]
        tail_count = count_list[-1][1]
        mid_change = count - head_count // 2 - tail_count // 2
        con_change = (head_count + tail_count) // 2
        print(head_count // 2 + tail_count // 2 + mid_change * K + con_change * (K-1))

Solution (Python):
s = input()
k = int(input())

tmp = 1
count = 0
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        tmp += 1
    else:
        count += tmp // 2
        tmp = 1
count += tmp//2

if len(set(s))==1:
    print(len(s)*k//2)
else:
    if s[0] != s[-1]:
        print(count*k)
    else:
        h = 0
        for i in s:
            if i != s[0]:
                break
            else:
                h += 1
        t = 0
        for i in s[::-1]:
            if i != s[-1]:
                break
            else:
                t += 1
        print(count*k-(h//2+t//2-(h+t)//2)*(k-1))

Solution (Python):



S = list(input())
k = int(input())

S_cnt = 0
repS = S[:]
for i in range(1, len(S)):
  if repS[i] == repS[i - 1]:
    repS[i] = "*"
    S_cnt += 1

if all(i == j for i, j in zip(S, S[1:])):
  ans = len(S) * k // 2
else:
  if S[0] != S[-1]:
    ans = S_cnt * k
  else:
    l = 0
    for s in S:
      if s == S[0]:
        l += 1
      else:
        break
    r = 0
    for s in S[::-1]:
      if s == S[-1]:
        r += 1
      else:
        break
    ans = S_cnt * k - (l // 2 + r // 2 - (l + r) // 2) * (k - 1)

print(ans)

Solution (Python):
S = list(input())
K = int(input())

N = len(S)

if len(set(S)) == 1: 
    tmp = K * N // 2
    print (tmp)
    exit()

if S[0] != S[-1]:
    count = 0
    for i in range(N - 1):
        if S[i] == S[i + 1]:
            S[i + 1] = '*'
            count += 1
    print (count * K)
else:
    i = 1
    while i < N:
        if S[i - 1] != S[i]:
            break
        i += 1
    
    j = 1
    while j < N:
        if S[N - j] != S[N - j - 1]:
            break
        j += 1
    
    ans = 0
    tmp = 1
    for k in range(i, N - j):
        if S[k] != S[k + 1]:
            ans += K * (tmp // 2)
            tmp = 1
        else:
            tmp += 1
    ans += K * (tmp // 2)
    ans += (K - 1) * ((i + j) // 2)
    ans += i // 2
    ans += j // 2
    print (ans)

Solution (Python):
import sys
input = sys.stdin.readline

s = list(input().rstrip())
k = int(input())
n = len(s)

group = []

cnt = 0

from collections import Counter
c = Counter(s)
if len(c) == 1:
    print(n*k//2)
    sys.exit()

for i in range(n):
    if i == 0:
        cnt += 1
        continue
    elif i == n - 1:
        if s[i] == s[i - 1]:
            cnt += 1
            group.append(cnt)
        else:
            group.append(cnt)
            group.append(1)
        continue
    
    if s[i] == s[i - 1]:
        cnt += 1
    else:
        group.append(cnt)
        cnt = 1
ans = 0
if k == 1:
    for i in group:
        ans += i//2
    print(ans)
    sys.exit()
from copy import deepcopy
first = deepcopy(group)
last = deepcopy(group)
if s[0] == s[-1]:
    first[-1] += last[0]
    last[0] = 0

for i in first:
    ans += i//2
for i in last:
    ans += i//2
if k == 2:
    print(ans)
    sys.exit()
mid = deepcopy(first)
if s[0] == s[-1]:
    mid[0] = 0
temp = 0
for i in mid:
    temp += i//2
ans += temp*(k - 2)
print(ans)

Solution (Python):
s = input()
k = int(input())

if len(set(s)) == 1:
    print((len(s)*k)//2)
    exit()

ss = s + s

shoko = 0
prev = ''
cnt = 0
for i in range(len(s)):
    if s[i] == prev:
        cnt += 1
    else:
        shoko += cnt // 2
        cnt = 1
    prev = s[i]
shoko += cnt // 2

kosa = 0
prev = ''
cnt = 0
for i in range(len(ss)):
    if ss[i] == prev:
        cnt += 1
    else:
        kosa += cnt // 2
        cnt = 1
    prev = ss[i]
kosa += cnt // 2
kosa -= shoko

print(shoko + (k-1)*kosa)

Solution (Python):
import sys

def solve():
    input = sys.stdin.readline
    S = list(input().strip("\n"))
    K = int(input())
    N = len(S)
    head, tail, joint = 0, 0, 0
    inside = 0
    if S[0] == S[N-1]: 
        isInitial = True
        current = S[0]
        currentLength = 1
        for i in range(1, N):
            if S[i] == current: currentLength += 1
            else:
                if isInitial: 
                    head = currentLength // 2
                    isInitial = False
                else: inside += currentLength // 2
                current = S[i]
                currentLength = 1
        if isInitial: 
            totalLength = N * K
            print(totalLength // 2)
        else:
            tail = currentLength // 2
            for i in range(N):
                if S[i] == current: currentLength += 1
                else: 
                    joint = currentLength // 2
                    break
            print(head + tail + joint * (K - 1) + inside * K)
    else:
        current = S[0]
        currentLength = 1
        for i in range(1, N):
            if S[i] == current: currentLength += 1
            else:
                inside += currentLength // 2
                current = S[i]
                currentLength = 1
        inside += currentLength // 2
        print(inside * K)   
    
    return 0

if __name__ == "__main__":
    solve()

Solution (Python):
def f(k):
    T = S*k
    N = len(T)
    cur = T[0]
    cnt = 1
    ans = 0
    for i in range(1,N):
        if T[i]==cur:
            cnt += 1
        else:
            ans += cnt//2
            cur = T[i]
            cnt = 1
    ans += cnt//2
    return ans
S = input().strip()
K = int(input())
ans1 = f(1)
ans2 = f(2)
ans3 = f(3)
d1 = f(2)-f(1)
d2 = f(3)-f(2)
k = (K-1)//2
if (K-1)%2==0:
    print(f(1)+(d1+d2)*k)
else:
    print(f(1)+(d1+d2)*k+d1)

Solution (Python):
import sys
input = sys.stdin.readline
from collections import *

S = input()[:-1]
K = int(input())

if S==S[0]*len(S):
    print(len(S)*K//2)
    exit()
    
l = []
cnt = 1

for i in range(len(S)-1):
    if S[i]!=S[i+1]:
        l.append((S[i], cnt))
        cnt = 1
    else:
        cnt += 1
    
l.append((S[-1], cnt))

if K==1 or l[0][0]!=l[-1][0]:
    ans = 0
    
    for _, c in l:
        ans += c//2*K
else:
    ans = (l[0][1]+l[-1][1])//2*(K-1)
    ans += l[0][1]//2
    ans += l[-1][1]//2
    
    for _, c in l[1:-1]:
        ans += c//2*K

print(ans)

Solution (Python):
s = list(str(input()))
k = int(input())

n = len(s)

cur = s[0]
L = []
cnt = 0
for i in range(n):
    if cur == s[i]:
        cnt += 1
    else:
        L.append(cnt)
        cur = s[i]
        cnt = 1
else:
    L.append(cnt)


if s[0] != s[-1]:
    C = 0
    for i in range(len(L)):
        if L[i] >= 2:
            C += L[i]//2
    ans = C*k
else:
    if len(L) >= 2:
        C = 0
        for i in range(1, len(L)-1):
            if L[i] >= 2:
                C += L[i]//2
        ans = C*k
        ans += L[0]//2+L[-1]//2+((L[0]+L[-1])//2)*(k-1)
    else:
        ans = (L[0]*k)//2
print(ans)

Solution (Python):
S = input()
k = int(input())
from itertools import groupby
x = y = 0
g = [len(list(v)) for k,v in groupby(S)]
for c in g:
    x += c//2

if S[-1] == S[0]:
    if g[-1]%2 == 0 or g[0]%2 == 0:
        pass
    elif len(S) == 1:
        y = k//2
    elif len(S) == g[0]:
        y = k//2
        
    else:
        y = k-1
print(x*k + y)

Solution (Python):
S = list(input())
K = int(input())

S1 = list(S)
cnt1 = 0
for i in range(1, len(S1)):
    if S1[i] == S1[i - 1]:
        S1[i] = '*'
        cnt1 += 1

S2 = list(S) * 2
cnt2 = 0
for i in range(1, len(S2)):
    if S2[i] == S2[i - 1]:
        S2[i] = '*'
        cnt2 += 1
        
S3 = list(S) * 3
cnt3 = 0
for i in range(1, len(S3)):
    if S3[i] == S3[i - 1]:
        S3[i] = '*'
        cnt3 += 1



ans = cnt1 + (cnt2 - cnt1) * (K // 2) + (cnt3 - cnt2) * ((K - 1) // 2)
print(ans)

Solution (Python):
s = list(input())
k = int(input())

if len(set(s)) == 1:
  print((len(s)*k)//2)
  exit()

def n_change(sss):
  s = sss.copy()
  x = 0
  for i in range(1, len(s)):
    if s[i] == s[i-1]:
      s[i] = "X"
      x += 1
  return x

print(n_change(s) + (k-1) * (n_change(2*s) - n_change(s)))

Solution (Python):
S = input()
K = int(input())
 
INNER = 0
  
if len(set(list(S))) == 1:
  print(K*len(S)//2)
else: 
  i = 0
  while i < len(S)-1:
    if S[i] == S[i+1]:
      INNER += 1
      i += 1
    i += 1
  
  if (S[0] != S[-1]):
    print(INNER*K)
  else:
    same_from_start = 0
    same_from_end = 0
    
    count_from_start = 0
    count_from_end = 0
    
    flag_same_from_start = 0
    flag_same_from_end = 0
    
    while flag_same_from_start == 0:
      if S[count_from_start] == S[count_from_start+1]:
        count_from_start += 1
      else:
        count_from_start += 1
        flag_same_from_start = 1
    
    while flag_same_from_end == 0:
      if S[len(S)-count_from_end-1] == S[len(S)-count_from_end-2]:
        count_from_end += 1
      else:
        count_from_end += 1
        flag_same_from_end = 1
    print(INNER*K + (K -1) * ((count_from_start+count_from_end)//2 -\
                              count_from_start//2- count_from_end//2))

Solution (Python):
s = list(input())
k = int(input())
x = []
ans = 0
cnt = 1
for i in range(len(s)):
    if i < len(s) - 1 and s[i] == s[i + 1]:
        cnt += 1
    else:
        ans += cnt // 2
        x.append(cnt)
        cnt = 1
cnt = 0
if s[0] == s[len(s) - 1]:
    cnt = x[0] // 2 + x[-1] // 2 - (x[0] + x[-1]) // 2

print(ans * k - cnt * (k - 1) if len(set(s)) != 1 else len(s) * k // 2)

Solution (Python):
from itertools import groupby
s = input()
k = int(input())
x = y = 0
g = [len(list(v)) for k,v in groupby(s)]

for c in g:
    x += c // 2
if s[-1] == s[0]:
    if g[-1] % 2 == 0 or g[0] % 2 == 0:
        pass
    elif len(s) == 1:
        y = k//2
    elif len(s) == g[0]:
        y = k//2
    else:
        y = k-1
print(x * k + y)

Solution (Python):
S = str(input())
K = int(input())

flag = []

N = len(S)
for i in range(N):
    if i == N - 1:
        flag.append(0)
        
            
        
            
    else:
        if S[i]==S[i+1]:
            flag.append(1)
        else:
            flag.append(0)

flag_2 = []
for i in range(N):
    flag_2.append(0)

for i in range(N):
    if flag[i] == 1:
        k = 0
        count = 0
        while((i == 0 or flag[i-1] == 0) and (i + k) < N and flag[i + k] == 1):
            k += 1
            count += 1
        flag_2[i] = count




ans = 0
cara = set()
for i in range(N):
    cara.add(S[i])
if len(cara) == 1:
    ans = N*K//2
else:

    if(S[0] != S[N-1]):
        for i in range(N-1):
            ans += (flag_2[i] + 1)//2

        ans = ans * K

    else:
        last = 0
        for i in range(N):
            if(S[0] != S[N - 1 - i]):
                last = N - i
                break
        
        temp = flag_2[0] + (N - last) + 1
        ans += temp//2 * (K - 1)
        for i in range(1, last):
            ans += (flag_2[i] + 1)//2 * K
        ans += (flag_2[0] + 1)//2
        ans += (flag_2[last] + 1)//2

print(ans)

Solution (Python):
def counter(s):
    cnt = 0
    flag = 0
    for i in range(1, len(s)):
        if flag == 1:
            flag = 0
        else:
            if s[i] == s[i-1]:
                cnt += 1
                flag = 1
    return cnt * k

s = str(input())
k = int(input())
if len(set(s)) == 1:
    ans = len(s) * k // 2
else:
    if k == 1 or s[0] != s[-1]:
        ans = counter(s)
    else:
        l = 0
        r = 0
        for i in range(len(s)):
            if s[i] == s[0]:
                l += 1
            else:
                break
        for j in range(len(s)-1, 0, -1):
            if s[j] == s[0]:
                r += 1
            else:
                break
        ans = counter(s[l:-r])
        ans += l // 2 + r // 2 + (l + r) // 2 * (k - 1)        
print(ans)

Solution (Python):
S = input()
K = int(input())


cnt = 0
for i in range(len(S)-1):
    if S[i] == S[i+1]:
        cnt += 0.5
    else:
        cnt = int(cnt+0.5)
cnt = int(cnt+0.5)


s3 = S*3
cnt_1 = 0
for i in range(len(s3)-1):
    if s3[i] == s3[i+1]:
        cnt_1 += 0.5
    else:
        cnt_1 = int(cnt_1+0.5)
cnt_1 = int(cnt_1+0.5)


ans = int((cnt_1-cnt*3)/2*(K-1)+0.5)+cnt*K
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

def main():
    S = input()
    K = int(input())
    if S[0] != S[-1]:
        ans = 0
        before = 0
        i = 0
        while i + 1 < len(S):
            while i + 1 < len(S) and S[i] == S[i + 1]:
                i += 1
            ans += (i + 1 - before)//2
            i += 1
            before = i
        print(ans*K)
    else:
        if len(set(S)) == 1:
            print(K*len(S)//2)
        else:
            i = 0
            while i + 1 < len(S) and S[i] == S[i + 1]:
                i += 1
            cut = i + 1
            L = (i + 1)//2
            i += 1
            
            before = i
            R = 0
            while i + 1 < len(S):
                while i + 1 < len(S) and S[i] == S[i + 1]:
                    i += 1
                R += (i + 1 - before)//2
                i += 1
                before = i
            
            S = S[cut:] + S[:cut]
            ans = 0
            before = 0
            i = 0
            while i + 1 < len(S):
                while i + 1 < len(S) and S[i] == S[i + 1]:
                    i += 1
                ans += (i + 1 - before)//2
                i += 1
                before = i
            ans = ans*(K - 1) + R + L
            print(ans)
if __name__ == '__main__':
    main()

Solution (Python):
s = list(input())
k = int(input())
num = len(s)
ans = 0

if len(set(s))==1:
    print(num*k//2)
else:
    now = s[0]
    cnt = 1
    fstflag = True
    for i in range(1,num):
        if s[i] == s[i-1]:
            cnt += 1
        else:
            ans += (cnt//2)*k
            if fstflag:
                fstflag = False
                begin = cnt
            cnt = 1
    ans += (cnt//2)*k
    if fstflag:
        fstflag = False
        begin = cnt
    if s[0] == s[-1]:
        end = cnt
        ans -= (begin//2)*(k-1)
        ans -= (end//2)*(k-1)
        ans += ((begin+end)//2)*(k-1)
    print(ans)