#include <bits/stdc++.h>

using namespace std;

typedef unsigned uint;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldbl;
typedef pair<int, int> pii;
typedef pair<uint, uint> puu;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<uint> vu;
typedef vector<ll> vll;
typedef vector<ull> vull;
typedef vector<pii> vpii;
typedef vector<puu> vpuu;
typedef vector<pll> vpll;
typedef vector<pull> vpull;
typedef vector<string> vstr;
typedef vector<double> vdbl;
typedef vector<ldbl> vldbl;
#define pb push_back
#define ppb pop_back
#define pfr push_front
#define ppfr pop_front
#define emp emplace
#define empb emplace_back
#define be begin
#define rbe rbegin
#define all(x) (x).be(), (x).end()
#define rall(x) (x).rbe(), (x).rend()
#define fir first
#define sec second
#define mkp make_pair
#define brif(cond) if (cond) break
#define ctif(cond) if (cond) continue
#define retif(cond) if (cond) return
void canhazfast() {ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);}
template<typename T> T gcd(T a, T b) {return b ? gcd(b, a%b) : a;}
template<typename T> T extgcd(T a, T b, T &x, T &y)
{
    T x0 = 1, y0 = 0, x1 = 0, y1 = 1;
    while (b) {
        T q = a/b; a %= b; swap(a, b);
        x0 -= q*x1; swap(x0, x1);
        y0 -= q*y1; swap(y0, y1);
    }
    x = x0; y = y0; return a;
}
int ctz(uint x) {return __builtin_ctz(x);}
int ctzll(ull x) {return __builtin_ctzll(x);}
int clz(uint x) {return __builtin_clz(x);}
int clzll(ull x) {return __builtin_clzll(x);}
int popcnt(uint x) {return __builtin_popcount(x);}
int popcntll(ull x) {return __builtin_popcountll(x);}
int bsr(uint x) {return 31^clz(x);}
int bsrll(ull x) {return 63^clzll(x);}

#define N 128
#define INF 1234567891
#define LLINF 123456789123456789LL

char s[N];
int dp[N][32];
ll cost[32][32];
ll tmp[32][32];
ll res[32][32];

void run(int n, int start)
{
    fill_n(dp[0], 26, INF);
    dp[0][start] = s[0] != start;
    for (int i = 1; i < n; ++i) {
        for (int j = 0; j < 26; ++j) {
            dp[i][j] = INF;
            for (int k = 0; k < 26; ++k) {
                ctif(k == j);
                dp[i][j] = min(dp[i][j], dp[i-1][k]);
            }
            if (j != s[i]) ++dp[i][j];
        }
    }
    for (int i = 0; i < 26; ++i) cost[start][i] = dp[n-1][i];
}

void mcopy(const ll src[][32], ll dst[][32])
{
    for (int i = 0; i < 26; ++i) copy_n(src[i], 26, dst[i]);
}

void mcomp(const ll a[][32], const ll b[][32], ll c[][32])
{
    for (int st = 0; st < 26; ++st) {
        for (int en = 0; en < 26; ++en) {
            c[st][en] = LLINF;
            for (int i = 0; i < 26; ++i) {
                for (int j = 0; j < 26; ++j) {
                    ctif(i == j);
                    c[st][en] = min(c[st][en], a[st][i]+b[j][en]);
                }
            }
        }
    }
}

int main()
{
    canhazfast();

    int n, k;
    ll ans = LLINF;

    cin >> s >> k;
    n = strlen(s);
    retif(n == 1) cout << k/2 << '\n', 0;
    for (int i = 0; i < n; ++i) s[i] -= 'a';
    for (int i = 0; i < 26; ++i) run(n, i);
    for (; k; k >>= 1) {
        if (k&1) {
            mcomp(res, cost, tmp);
            mcopy(tmp, res);
        }
        mcomp(cost, cost, tmp);
        mcopy(tmp, cost);
    }
    for (int i = 0; i < 26; ++i) {
        for (int j = 0; j < 26; ++j) ans = min(ans, res[i][j]);
    }
    cout << ans << '\n';

    return 0;
}


Measured memory_bytes: 256