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 C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
class ConnectionandDisconnection
{
	static void Main(string[] args)
	{
		string S = Console.ReadLine();
		int K = int.Parse(Console.ReadLine());
		Console.WriteLine(disconnect(S, K));
	}

	static long disconnect(string conn_str, int loop) {
		long totalCounter = 0;
		if (conn_str.Length==0) return 0;
		else if (conn_str.Length==1) return (long)loop/2;
		// 先頭
		DisconnectedParts first = getDisconnectedStringForOneLoop(conn_str, null, true, false);
		totalCounter += (long)first.counter;
		// 先頭・最終以外
		string last_targetPrevString = first.disconnected_str;
		if (loop>2) {
    	    DisconnectedParts part1 = getDisconnectedStringForOneLoop(conn_str, last_targetPrevString, false, false);
    	    totalCounter += (long)part1.counter;
    	    last_targetPrevString = part1.disconnected_str;
    	    if (loop>3) {
    	        DisconnectedParts part2 = getDisconnectedStringForOneLoop(conn_str, last_targetPrevString, false, false);
    	        if (part1.disconnected_str!=part2.disconnected_str) {
    	            // 偶数のとき
    	            if ((loop%2)==0) {
    	               totalCounter += (long)(part1.counter*((loop-2)/2))+(part2.counter*((loop-2)/2))-part1.counter;
    	               last_targetPrevString = part2.disconnected_str;
    	               //Console.WriteLine((long)(part1.counter*((loop-2)/2))+(part2.counter*((loop-2)/2))-part1.counter);
    	            }
    	            // 奇数のとき
    	            else {
    	                totalCounter += (long)(part1.counter*((loop-2)/2)+1)+(part2.counter*((loop-2)/2))-(long)part1.counter;
    	            }
    	        }
    	        else {
    	            totalCounter += (long)part1.counter*(loop-3);
    	        }
    	    }
		}
		// 最終
		if (loop>1) {
    		DisconnectedParts last = getDisconnectedStringForOneLoop(conn_str, last_targetPrevString, false, true);
    		totalCounter += (long)last.counter;
		}
		return totalCounter;
	}
	
    static DisconnectedParts getDisconnectedStringForOneLoop(string conn_str, string prev_str, Boolean isStartForLoop,  Boolean isEndForLoop) {
        int size = conn_str.Length;
		string alph = "abcdefghijklmnopqrstuvwxyz";

        string disconnected_str = "";
        DisconnectedParts part = new DisconnectedParts();

        if (isStartForLoop) {
        	disconnected_str += conn_str[0];
        }
        else {
            if (conn_str[0]==prev_str[size-1]) {
                int index = (getIndexNum(alph, conn_str[0])+1)%alph.Length;
                while (prev_str[size-1]==alph[index]||alph[index]==conn_str[1]) {
    				index = (index+1)%alph.Length;
    				//Console.WriteLine(index);
    			}
    			part.count();
    			disconnected_str += alph[index];
            }
            else {
                disconnected_str += conn_str[0];
            }
        }
        
        for (int i=0;i<size-2;i++) {
    		if (disconnected_str[i]==conn_str[i+1]) {
    			int index = (getIndexNum(alph, conn_str[i+1])+1)%alph.Length;
    			while (disconnected_str[i]==alph[index]||alph[index]==conn_str[i+2]) {
    				index = (index+1)%alph.Length;
    				//Console.WriteLine(index);
    			}
    			part.count();
    			disconnected_str += alph[index];
    			//Console.WriteLine(disconnected_str);
    		}
    		else {
    			disconnected_str += conn_str[i+1];
    		}
    	}
		
		if (!isEndForLoop) {
		// 終端文字の判定
    		if (disconnected_str[size-2]==conn_str[size-1]) {
    		    int index = (getIndexNum(alph, conn_str[size-1])+1)%alph.Length;
                while (disconnected_str[size-2]==alph[index]||alph[index]==conn_str[0]) {
    				index = (index+1)%alph.Length;
    				// Console.WriteLine(index);
    			}
    			part.count();
    			disconnected_str += alph[index];
    			//Console.WriteLine(disconnected_str);
    		}
    		else {
    		    disconnected_str += conn_str[size-1];
    		}
		}
		else {
		    if (disconnected_str[size-2]==conn_str[size-1]) {
		        int index = (getIndexNum(alph, conn_str[size-1])+1)%alph.Length;
                while (disconnected_str[size-2]==alph[index]) {
    				index = (index+1)%alph.Length;
    				// Console.WriteLine(index);
    			}
    			part.count();
    			disconnected_str += alph[index];
    			//Console.WriteLine(disconnected_str);
    		}
    		else {
    		    disconnected_str += conn_str[size-1];
    		}
		}
		part.setDiscpnnectedString(disconnected_str);
		//Console.WriteLine(disconnected_str);
		return part;
    }

	static int getIndexNum(string basestr, char target) {
		for (int i=0;i<basestr.Length;i++) {
			if (basestr[i]==target) {
				return i;
			}
		}
		return -1;
	}
}

class DisconnectedParts {
    public int counter;
    public string disconnected_str;
    public DisconnectedParts() {
        counter = 0;
        disconnected_str = "";
    }
    public void count() {
        counter++;
    }
    public void setDiscpnnectedString(string disconn_str) {
        this.disconnected_str = disconn_str;
    }
}