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)

#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>

using namespace std ;
using namespace  __gnu_pbds ;
using namespace __gnu_cxx ;

typedef long long ll ;
typedef long double ld ;

typedef pair<int, int> pii ;
typedef pair<ll,ll> pll ;

typedef vector<int> vi ;
typedef vector<ll> vll ;
typedef vector<pii> vpi ;
typedef vector<pll> vpl ;
typedef vector<vi> vvi ;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define rep(i,a,b) for (int i = (a); i <= (b); i++)
#define per(i,b,a) for (int i = (b); i >= (a); i--)

#define mp make_pair
#define eb emplace_back
#define pb push_back
#define fi first
#define se second

#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)

const int MOD1 = 1000000007 ;
const int MOD2 = 998244353 ;
const ll INFL = 1e18 ;
const int INFI = 2e9 ;
const int MAXN = 205 ;
const int LOGN = 22 ;
const int MAXM = (1 << LOGN) ;
const int ALPHA = 30 ;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()) ;

ll modpow(ll a, ll b, int MOD) {
    ll res = 1 ;
    while(b) {
        if(b&1) {
            res = res*a ;
            res %= MOD ;
        }
        a *= a ;
        a %= MOD ;
        b >>= 1 ;
    }
    return res%MOD ;
}
void upmin(int &a, int b) {
    if(a < b) {
        a = b ;
    }
}
void relax(int &a, int b) {
    if(a > b) {
        a = b ;
    }
}
ll add(ll a, ll b, int MOD) {
    a += b ;
    if(a >= MOD) {
        a -= MOD ;
    }
    return  a ;
}
ll subtract(ll a, ll b, int MOD) {
    a -= b ;
    if(a < 0) {
        a += MOD ;
    }
    return a ;
}
ll multiply(ll a, ll b, int MOD) {
    a *= b ;
    a %= MOD ;
    return a ;
}
string s ;
int n;
ll k ;
int dp[MAXN][ALPHA][ALPHA] ;
int main() {
    ios::sync_with_stdio(false) ;
    cin.tie(NULL) ;
    cin >> s >> k ;
    n = sz(s) ;
    rep(i,0,n-1) rep(j,0,25) rep(jj,0,25) dp[i][j][jj] = n + 15 ;
    rep(j,0,25) {
        int x = int(s[0] - 'a') ;
        int cost = 1 ;
        if(j == x) {
             cost = 0 ;
        }
        dp[0][j][j] = cost ;
    }
    rep(i,1,n-1) {
        rep(j,0,25) {
            rep(jj,0,25) {
               int cost = 1 ;
               if(j == int(s[i] - 'a')) {
                   cost = 0 ;
               }
               rep(jjj,0,25) {
                  if(j == jjj) continue ;
                  dp[i][j][jj] = min(dp[i-1][jjj][jj] + cost, dp[i][j][jj]) ;
               }
            }
        }
    }
    int best = n + 15 ;
    bool good ;
    rep(j,0,25) {
       rep(jj,0,25) {
          if(best > dp[n-1][j][jj]) {
              best = dp[n-1][j][jj] ;
              if(j == jj) {
                  good = false ;
              }
              else {
                  good = true ;
              }
          }
       }
    }
    ll res ;
    if(good) {
        res = k*best ;
        cout << res << endl ;
        return 0 ;
    }
    res = k*best + k - 1 ;
    int pbest = best ;
    best = n + 15 ;
    rep(j,0,25) {
        rep(jj,0,25) {
           if(j != jj) {
                best = min(best, dp[n-1][j][jj]) ;
           }
        }
    }
    res = min(res, (k-1)*best + pbest*1LL) ;
    cout << res << endl ;
    return 0 ;
}