#include<bits/stdc++.h>
        #define ll  long long
        #define speed_up    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
        #define rep(i,a,b)  for(ll i=a;i<b;i++)
        #define pb push_back
        #define Max(x,y,z) max(x,max(y,z))
        #define f first
        #define s second
        #define sz(x)       (ll)x.size()
        #define pi 3.1415926536
        #define mod 1000000007
        #define MOD 998244353
        #define maxi 100000000000000000
        using namespace std;
       const int N=1e5+5;
       ll n;
       /*ll dp[N][25],dp2[N][25];
       ll logcal[N];*/
/*       void update(ll x,ll value,ll pos)
{
    while(pos<=n)
    {
        bit[x][pos]+=value;
        pos+=(pos)&(-pos);
    }
}
ll query(ll x,ll pos)
{  ll ans=0;
    while(pos>0)
    {
        ans+=bit[x][pos];
        pos-=(pos)&(-pos);
    }
    return ans;
}*/
    /*
    bool vis[N];
    ll cnt=0;
    void dfs(ll x)
    {
        vis[x]=1;
        cnt++;
        for(auto y:adj[x])
            if(!vis[y]) dfs(y);
    }*/
      /* ll dfs(ll x,bool vis[],bool rec[])
    {   //cout<<x<<" ";
        vis[x]=1;
        rec[x]=1;
        for(auto y:adj[x])
            if(!vis[y] && dfs(y,vis,rec)) return true;
            else if(rec[y]) return true;
        rec[x]=false;
        return false;
    }*/
        /*bool isprime(ll x)
        {
            for(ll i=2;(i*i)<=x;i++)
            {
                if(x%i==0) return 0;
            }
            return 1;
        }*/
        /*vector<ll> _lps(string &t)
        {   ll n=t.size();
            vector<ll> lps(n+1,0);
            ll i=1,len=0;
            while(i<n)
            {
                if(t[i]==t[len])
                {
                    len++;
                    lps[i]=len;
                    i++;
                }
                else
                {
                    if(len!=0)
                        len=lps[len-1];
                    else
                        lps[i++]=0;
                }
            }
            return lps;
        }
        ll KMP(string &s,string &t)
        {
            ll n=s.size();
            ll m=t.size();
            vector<ll> lps=_lps(t);
            ll i=max(0LL,n-m),j=0;
            while(i<n)
            {
                if(s[i]==t[j])
                {
                    i++;
                    j++;
                }
                else
                    if(j!=0) j=lps[j-1];
                    else i++;
            }
            return j;

        }*/
        void solve()
        {
                    ll m,y,z,h,b,c,d,e,g,k1,k2,p,x,k,q;
                    string s;
                    cin>>s;
                    cin>>k;
                    n=sz(s);
                    set<ll> st;
                    rep(i,0,n) st.insert(s[i]);
                    if(st.size()==1) {cout<<((n*k)/2);return;}
                    if(k==1)
                    {   ll sum1=0;
                        for(ll i=1;i<n;)
                        {
                            if(s[i]==s[i-1])
                            {
                                sum1+=1;
                                i+=2;
                            }
                            else i++;
                        }
                        cout<<sum1<<"\n";
                        return;
                    }
                    if(s[0]==s[n-1])
                    {   string s1=s+s;
                        ll sum1=0,sum2=0;
                        for(ll i=1;i<n;)
                        {
                            if(s[i]==s[i-1])
                            {
                                sum1+=1;
                                i+=2;
                            }
                            else i++;
                        }
                        for(ll i=1;i<2*n;i++)
                        {
                            if(s1[i]==s1[i-1])
                            {
                                sum2+=1;
                                i++;
                            }
                        }
                        //cout<<sum2<<" ";
                        if(k==2)
                        {
                            cout<<sum2<<"\n";
                            return;
                        }
                        ll diff=sum2-sum1;
                        sum1+=((k-1)*(diff));
                        cout<<sum1<<"\n";
                        return;

                    }
                    else
                    {
                        ll sum1=0,sum2=0;
                        for(ll i=0;i<n-1;)
                        {
                            if(s[i]==s[i+1])
                            {sum1+=1;i+=2;}
                            else i+=1;
                        }
                        for(ll i=n-1;i>=1;)
                        {
                            if(s[i]==s[i-1])
                            {
                                sum2+=1;i-=2;
                            }
                            else i--;
                        }
                        cout<<(k*min(sum1,sum2));
                        return;
                    }


}

                int main()
                {
                    speed_up
                    ll t=1;
                    //call();
                    //cin>>t;
                    while(t--)
                        solve();
                }


Measured memory_bytes: 256