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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;

// iterator
#define REP(i,from, to) for(lli i=from;i<to;i++)
#define REPE(i,from, to) for(lli i=from;i<=to;i++)
#define REP_R(i,from, to) for(lli i=from;i>to;i--)
#define REPE_R(i,from, to) for(lli i=from;i>=to;i--)
#define REPIT(it,container) for(auto it = container.begin(); it != container.end(); it++)
#define REPIT_R(it,container) for(auto it = container.rbegin(); it != container.rend(); it++)

// input
#define cin1(x)             cin >> x
#define cin2(x, y)          cin >> x >> y
#define cin3(x, y, z)       cin >> x >> y >> z
#define ncin1(n, x)         REP(i, 0, n) {cin1(x[i]);}
#define ncin2(n, x, y)      REP(i, 0, n) {cin2(x[i], y[i]);}
#define ncin3(n, x, y, z)   REP(i, 0, n) {cin3(x[i], y[i], z[i]);}

// output
#define cout1(x)         cout << #x << ": " << x << endl;
#define cout2(x, y)      cout << #x << ": " << x << ", " << #y << ": " << y << endl;
#define cout3(x, y, z)   cout << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << endl;;
#define ncout1(n, x)     REP(i, 0, n) {cout << #x << "[" << i << "]: "<< x[i] << endl;}

#define coutp(p)         cout << #p << ":" <<  " (" << p.first << ", " << p.second << ")" << endl;

// sort
#define sort_r(x, y)        sort(x, y, greater<lli>()); // 降順(5,4,3,,,)

#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))

typedef long long int lli;
typedef pair<lli, lli> P;
typedef tuple<lli, lli, lli> tup;

int main() {
    string s;
    unsigned long long k;
    cin1(s);
    cin1(k);
    if (s.length() == 1) {
        if (k % 2 == 0)
            cout << k/2 << endl;
        else
            cout << (k-1)/2<<endl;
        return 0;
    }
    unsigned long long int ans = 0;
    unsigned long long int  con = 1;
    //cout1(s.length());
    REP(i,0,s.length()-1) {
        if (s[i] == s[i+1]) {
            con++;
        }
        else {
            if (con % 2 == 0) {
                //cout1(con/2);
                ans += con /2 ;
            }
            else if (con != 1) {
                //cout1((con-1)/2);
                ans += (con - 1)/2;
            }
            con = 1;
        }
    }
    if (con != 1) {
        if (con % 2 == 0) {
            //cout1(con/2);
            ans += con /2 ;
        }
        else if (con != 1) {
            //cout1((con-1)/2);
            ans += (con - 1)/2;
        }
    }
    unsigned long long first_count = 1;
    unsigned long long second_count = 1;
    REP(i,0,s.length()-1) {
        if (s[i] != s[i+1])
            break;
        first_count++;
    }
    REP_R(i,s.length()-1,0) {
        if (s[i] != s[i-1])
            break;
        second_count++;
    }

    if (first_count == second_count && first_count == s.length()) {
        if (first_count % 2 == 0) {
            cout << ans * k << endl;
        }
        else {
            cout << ans * k + k/2 << endl;
        }
        return 0;
    }


    unsigned long long x = 0;
    unsigned long long y = 0;
    unsigned long long z = 0;
    if (first_count != 1 && second_count != 1) {

        if (first_count != 1) {
            ans -= first_count / 2;
        }
        if (second_count != 1) {
            ans -= second_count / 2;
        }
        //cout1(first_count);
        //cout1(second_count);

        x = first_count / 2;
        z = second_count / 2;

        y = (first_count + second_count) * (k-1) / 2;

    }

    //if (first_count == second_count && first_count == s.length()) {
    //    if (k % 2 == 0)
    //        x = k/2;
    //    else
    //        x = (k-1)/2;
    //}
    //else if ((s[0] == s[s.length()-1] && first_count == 1 && second_count == 1) ||
    //    (s[0] == s[s.length()-1] && first_count == 1 && second_count % 2 == 1) ||
    //    (s[0] == s[s.length()-1] && first_count % 2 == 1 && second_count == 1) )
    //    x = (k-1);
    //else if (s[0] == s[s.length()-1] && (first_count % 2 == 1 && second_count % 2 == 1)) {
    //    if (k % 2 == 0)
    //        x = k/2;
    //    else
    //        x = (k+1)/2;
    //}
    cout << ans * k + x + y + z << endl;
}