#include <bits/stdc++.h>

#define       pb                 push_back
#define       mx                 200005
#define       MOD                1000000007
#define 	  optimize           ios::sync_with_stdio(0);cin.tie(0);
#define 	  inf                0x3f3f3f3f
#define       READ               freopen("in.txt", "r", stdin);
#define       WRITE              freopen("out.txt", "w", stdout);

using namespace std;

typedef       long long int      ll;
typedef       vector<int> 	     vi;

int dx[] = {0, 0, +1, -1};
int dy[] = {+1,-1, 0,  0};
/*============GRAPH MOVES==================*/
//const int fx[] = {1, -1, 0, 0};
//const int fy[] = {0, 0, 1, -1};
//const int fx[] = {0, 0, 1, -1, -1, 1, -1, 1};  // Kings move
//const int fy[] = {-1, 1, 0, 0, 1, 1, -1, -1};  // Kings move
//const int fx[] = {-2, -2, -1, -1, 1, 1, 2, 2}; // Knights move
//const int fy[] = {-1, 1, -2, 2, -2, 2, -1, 1}; // Knights move
/*=========================================*/

inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
inline bool isLeapYear(ll year) { return (year%400==0) || (year%4==0 && year%100!=0); }
inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); }
inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
inline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
inline ll modInverse(ll a) { return modPow(a, MOD-2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }
inline ll mod(ll a, ll m){ if(a<0) return m-(abs(a)%m); else if(a<m) return a; else return (a%m); }
inline string tstring(ll a) {stringstream ss; ss << a; string str = ss.str(); return str; }

int main()
{
    ll a, cn = 1, ans = 0, an, in1, in2, cnt=0;
    string s;

    cin >> s >> a;

    in2 = s.size();

    if(a==1)
    {
        char c = s[0];

        for(int i = 1; i < s.size(); i++)
        {
            if(s[i]==c) cn++;
            else
            {
                ans += (cn/2);
                cn = 1;
            }

            c = s[i];
        }

        ans += cn/2;

        cout << ans*a << endl;
    }
    else
    {
        ans = 0;

        char c = s[0];

        for(int i = 0; i < s.size(); i++)
        {
            if(s[i]==c)
            {
                cnt++;
                in1 = i+1;
            }
            else break;
        }

        for(int i = s.size()-1; i >= 0; i--)
        {
            if(s[i]==c)
            {
                cnt++;
                in2 = i;
            }
            else break;
        }

        if(cnt==s.size()*2)
        {
            cout << (a*s.size())/2 << endl;
            return 0;
        }

        ans += (cnt/2)*(a-1);

//        cout << ans << endl;


        c = s[0];
        cn = 1, an = 0;
        for(int i = 1; i < in2; i++)
        {
            if(s[i]==c) cn++;
            else
            {
                an += (cn/2);
                cn = 1;
            }

            c = s[i];
        }
        an += cn/2;
        ans += an;

        c = s[in1];
        cn = 1, an = 0;
        for(int i = in1+1; i < s.size(); i++)
        {
            if(s[i]==c) cn++;
            else
            {
                an += (cn/2);
                cn = 1;
            }

            c = s[i];
        }
        an += cn/2;
        ans += an;

        if(a>=2)
        {
            c = s[in1];
            cn = 1, an = 0;

            for(int i = in1+1; i < in2+1; i++)
            {
                if(s[i]==c) cn++;
                else
                {
                    an += (cn/2);
                    cn = 1;
                }

                c = s[i];
            }
            an += cn/2;
            ans += an*(a-2);
        }


        cout << ans << endl;
    }

    return 0;
}



Measured memory_bytes: 256