#include <iostream>
#include <string>
#include <algorithm>
#include <cassert>
#include <limits>
#include <numeric>
#include <type_traits>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <functional>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <complex>
#include <climits>
#include <time.h>

using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<string> vs;
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define REP(i,n)  FOR(i,0,n)

ll gcd(ll a, ll b) {
 	while (a && b) {
 		if (a >= b) a %= b;
 		else b %= a;
  	}
  	return a + b;
}
ll ketasum(ll n) { //桁の数字全ての和を返す 例: 112 -> 4
	ll sum=0;
	if (n<0) return 0;
	while (n>0) {
		sum += n%10;
		n /= 10;
	}
	return sum;
}

typedef pair<ll, ll> pair_t;

bool comp(const pair_t &a, const pair_t &b) {
        return a.first < b.first;
}

void sort2vectors(vector<ll> &av, vector<ll> &bv)
{ // ふたつの配列をまとめてそーと A[2,1] B[3,4] -> 1,4 2,3
        ll n = av.size();
        vector<pair_t> p(n);
        vector<ll> av2(n), bv2(n);
        for (ll i = 0; i < n; i++)
                p[i] = make_pair(av[i], bv[i]);
        sort(p.begin(), p.end(), comp);
        for (ll i = 0; i < n; i++) {
                av2[i] = p[i].first;
                bv2[i] = p[i].second;
        }
        av = av2;
        bv = bv2;
}
ll facctorialMethod(ll k){ //階乗
	ll s;
	if(k > 1){
		s = k*facctorialMethod(k-1);
	} else {
		s = 1;
	}
	return s;
}

map< int64_t, ll > prime_factor(int64_t n) {
	map< int64_t, ll > ret;
 	for(int64_t i = 2; i * i <= n; i++) {
    	while(n % i == 0) {
      		ret[i]++;
      		n /= i;
    	}
  	}
  	if(n != 1) ret[n] = 1;
  	return ret;
}
const ll mod = 1000000007;
const ll INF = 1e9;

// 変数定義start(vectorはresizeしようね)
	string S;
	ll K;
// 変数定義end

void solve(){
    cin >> S >> K;
    ll ans = 0;
    ll anskari = 0;
    REP(i, S.size()-1){
    	if(S.substr(i, 1) == S.substr(i+1,1)){
    		anskari++;
    		i++;
    	}
    }
    ll countback = 0;
    ll countfront = 0;
    bool flag = true;
    for(ll i = S.size() - 1; i >= 0; i--){
    	if(flag){
	    	for(ll j = 0; j < S.size(); j++){
	    		if(S.substr(j, 1) == S.substr(0, 1)){
	    			countfront++;
	    		} else {
	    			flag = false;
	    			break;
	    		}
	    	}
	    }
    	if(S.substr(i, 1) == S.substr(S.size()-1, 1)){
    		countback++;
    	} else {
    		break;
    	}
    }
    if(!flag){
		if((countfront + countback) % 2 == 0 && S.substr(0, 1) == S.substr(S.size()-1, 1)){
		    ans += (K-1)*(countfront+countback)/2;
		}
		if((countfront + countback) % 2 == 1 && S.substr(0, 1) == S.substr(S.size()-1, 1)){
		    ans += (K-1)*floor((countfront+countback)/2);
		}
		if((countfront + countback) % 2 == 0 && S.substr(0, 1) != S.substr(S.size()-1,1)){
			ans += K*(floor(countfront/2)+floor(countback/2))-floor(countfront/2) - floor(countback/2);
		}
		if((countfront + countback) % 2 == 1 && S.substr(0, 1) != S.substr(S.size()-1,1)){
			ans += K*floor((countfront+countback)/2) - floor(countfront/2) - floor(countback/2);
		}

		anskari -= (floor(countfront/2) + floor(countback/2));
		ans += anskari * K;
		ans += floor(countfront/2) + floor(countback/2);
	} else {
		if(S.size()*K % 2 == 0){
			ans = S.size()*K/2;
		} else {
			ans = floor(S.size()*K/2);
		}
	}
    cout << ans << endl;
}

int main(){

    //clock_t start = clock();

    cout << std::fixed << std::setprecision(10);
  	ios::sync_with_stdio(false);
  	cin.tie(nullptr);
  	solve();
  	//clock_t end = clock();

    //const double time = static_cast<double>(end - start) / CLOCKS_PER_SEC * 1000.0;
    //printf("time %lf[ms]\n", time);
}


Measured memory_bytes: 256