Buggy python submission (verdict: Runtime Error):
S=input()
K=int(input())
L=len(S)
cnt=0
ren=1

if L==1:
  ans= K//2
elif L==2:
  if S[0]==S[1]:
    ans = K
  else:
    ans = 0
else:  #|S|>=3が保証
  if S[0]!=S[-1]:
    for i in range(1,L):  
      if S[i]==S[i-1]:
        ren +=1
        if i==L-1:
          cnt += ren//2  
      else:
        cnt += ren//2
        ren=1
    ans=cnt*K
  else:
    head=0
    tail=0
    i=0
    j=0
    while S[i]==S[0]:
      head += 1
      i +=1
    while S[-1-j]==S[-1]:
      tail += 1
      j += 1
    #print(head,tail)
    if L-head-tail<0:
      ans=K*L//2
    elif L-head-tail==0:
      ans= K*( (head//2)+(tail//2) )
    else:  
      for k in range(head,L-tail):  
        #print("k=",k,"S[k]=",S[k])
        if S[k]==S[k-1]:
          ren +=1
          if k==L-tail-1:
            cnt += ren//2  
        else:
          cnt += ren//2
          ren=1
      #print("cnt",cnt)   
      
    ans=K*cnt + (K-1)*(head+tail)//2 + head//2 + tail//2
    
print(ans)


Fixed submission:
S=input()
K=int(input())
N=len(S)

if S.count(S[0])==N:
  ans= K*N//2
else:
  h=0
  for i in range(N):
    if S[i]==S[0]:
      h +=1
    else:
      break
  t=0
  for i in range(N-1,-1,-1):
    if S[i]==S[-1]:
      t +=1
    else:
      break
  ren=1
  cnt=0
  for i in range(h,N-t):
    if S[i]==S[i-1]:
      ren +=1
      if i==N-t-1:
        cnt += ren//2
    else:
      cnt += ren//2
      ren=1    
      
  if S[0]==S[-1]:
    #print(h//2, (K-1)*((h+t)//2), t//2 ,K*cnt)
    ans= h//2 + (K-1)*((h+t)//2) + t//2 + K*cnt
  else:
    #print(h//2,cnt,t//2)
    ans= (h//2 + cnt + t//2)*K
print(ans)

Buggy python submission (verdict: Runtime Error):
S=input()
K=int(input())
L=len(S)
cnt=0
ren=1

if L==1:
  ans= K//2
elif L==2:
  if S[0]==S[1]:
    ans = K
  else:
    ans = 0
else:  #|S|>=3が保証
  if S[0]!=S[-1]:
    for i in range(1,L):  
      if S[i]==S[i-1]:
        ren +=1
        if i==L-1:
          cnt += ren//2  
      else:
        cnt += ren//2
        ren=1
    ans=cnt*K
  else:
    head=0
    tail=0
    i=0
    j=0
    while S[i]==S[0]:
      head += 1
      i +=1
    while S[-1-j]==S[-1]:
      tail += 1
      j += 1
    #print(head,tail)
    if L-head-tail<=0:
      ans=K*L//2
    else:  
      for k in range(head,L-tail):  
        #print("k=",k,"S[k]=",S[k])
        if S[k]==S[k-1]:
          ren +=1
          if k==L-tail-1:
            cnt += ren//2  
        else:
          cnt += ren//2
          ren=1
      #print("cnt",cnt)   
      
    ans=K*cnt + (K-1)*(head+tail)//2 + head//2 + tail//2
    
print(ans)


Fixed submission:
S=input()
K=int(input())
N=len(S)

if S.count(S[0])==N:
  ans= K*N//2
else:
  h=0
  for i in range(N):
    if S[i]==S[0]:
      h +=1
    else:
      break
  t=0
  for i in range(N-1,-1,-1):
    if S[i]==S[-1]:
      t +=1
    else:
      break
  ren=1
  cnt=0
  for i in range(h,N-t):
    if S[i]==S[i-1]:
      ren +=1
      if i==N-t-1:
        cnt += ren//2
    else:
      cnt += ren//2
      ren=1    
      
  if S[0]==S[-1]:
    #print(h//2, (K-1)*((h+t)//2), t//2 ,K*cnt)
    ans= h//2 + (K-1)*((h+t)//2) + t//2 + K*cnt
  else:
    #print(h//2,cnt,t//2)
    ans= (h//2 + cnt + t//2)*K
print(ans)

Buggy python submission (verdict: Runtime Error):
S=input()
K=int(input())
L=len(S)
cnt=0
ren=1

if L==1:
  ans= K//2
elif L==2:
  if S[0]==S[1]:
    ans = K
  else:
    ans = 0
else:  #|S|>=3が保証
  if S[0]!=S[-1]:
    for i in range(1,L):  
      if S[i]==S[i-1]:
        ren +=1
        if i==L-1:
          cnt += ren//2  
      else:
        cnt += ren//2
        ren=1
    ans=cnt*K
  else:
    head=0
    tail=0
    i=0
    j=0
    while S[i]==S[0]:
      head += 1
      i +=1
    while S[-1-j]==S[-1]:
      tail += 1
      j += 1
    #print(head,tail)
    elif L-head-tail<=0:
      ans=K*L//2
    else:  
      for k in range(head,L-tail):  
        #print("k=",k,"S[k]=",S[k])
        if S[k]==S[k-1]:
          ren +=1
          if k==L-tail-1:
            cnt += ren//2  
        else:
          cnt += ren//2
          ren=1
      #print("cnt",cnt)   
      
    ans=K*cnt + (K-1)*(head+tail)//2 + head//2 + tail//2
    
print(ans)


Fixed submission:
S=input()
K=int(input())
N=len(S)

if S.count(S[0])==N:
  ans= K*N//2
else:
  h=0
  for i in range(N):
    if S[i]==S[0]:
      h +=1
    else:
      break
  t=0
  for i in range(N-1,-1,-1):
    if S[i]==S[-1]:
      t +=1
    else:
      break
  ren=1
  cnt=0
  for i in range(h,N-t):
    if S[i]==S[i-1]:
      ren +=1
      if i==N-t-1:
        cnt += ren//2
    else:
      cnt += ren//2
      ren=1    
      
  if S[0]==S[-1]:
    #print(h//2, (K-1)*((h+t)//2), t//2 ,K*cnt)
    ans= h//2 + (K-1)*((h+t)//2) + t//2 + K*cnt
  else:
    #print(h//2,cnt,t//2)
    ans= (h//2 + cnt + t//2)*K
print(ans)

Buggy python submission (verdict: Runtime Error):
S=input()
K=int(input())
L=len(S)
cnt=0
ren=1

if L==1:
  ans= K//2
elif L==2:
  if S[0]==S[1]:
    ans = K
  else:
    ans = 0
else:  #|S|>=3が保証
  if S[0]!=S[-1]:
    for i in range(1,L):  
      if S[i]==S[i-1]:
        ren +=1
        if i==L-1:
          cnt += ren//2  
      else:
        cnt += ren//2
        ren=1
    ans=cnt*K
  else:
    head=0
    tail=0
    i=0
    j=0
    while S[i]==S[0]:
      head += 1
      i +=1
    while S[-1-j]==S[-1]:
      tail += 1
      j += 1
    #print(head,tail)
    if L-head-tail==0:
      print(K*L//2)
    elif L-head-tail<0:
      ans=K*L//2
    else:  
      for k in range(head,L-tail):  
        #print("k=",k,"S[k]=",S[k])
        if S[k]==S[k-1]:
          ren +=1
          if k==L-tail-1:
            cnt += ren//2  
        else:
          cnt += ren//2
          ren=1
      #print("cnt",cnt)   
      
    ans=K*cnt + (K-1)*(head+tail)//2 + head//2 + tail//2
    
print(ans)


Fixed submission:
S=input()
K=int(input())
N=len(S)

if S.count(S[0])==N:
  ans= K*N//2
else:
  h=0
  for i in range(N):
    if S[i]==S[0]:
      h +=1
    else:
      break
  t=0
  for i in range(N-1,-1,-1):
    if S[i]==S[-1]:
      t +=1
    else:
      break
  ren=1
  cnt=0
  for i in range(h,N-t):
    if S[i]==S[i-1]:
      ren +=1
      if i==N-t-1:
        cnt += ren//2
    else:
      cnt += ren//2
      ren=1    
      
  if S[0]==S[-1]:
    #print(h//2, (K-1)*((h+t)//2), t//2 ,K*cnt)
    ans= h//2 + (K-1)*((h+t)//2) + t//2 + K*cnt
  else:
    #print(h//2,cnt,t//2)
    ans= (h//2 + cnt + t//2)*K
print(ans)