#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;
    }

    if (s[0] != s[s.length()-1]) {
        cout << ans * k << endl;
        return 0;
    }

    unsigned long long x = 0;
    unsigned long long y = 0;
    unsigned long long z = 0;

    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)  / 2)* (k-1);
    //cout1(ans);
    //cout3(x,y,z);

    //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;
}


Measured memory_bytes: 256