#include <bits/stdc++.h>
#define loop(n) for (int ngtkana_is_genius = 0; ngtkana_is_genius < int(n); ngtkana_is_genius++)
#define rep(i, begin, end) for(int i = int(begin); i < int(end); i++)
#define all(v) v.begin(), v.end()
#define rand(l, r) std::uniform_int_distribution<>(l, r)(mt)
using lint = long long;
auto cmn = [](auto& a, auto b){if (a > b) {a = b; return true;} return false;};
auto cmx = [](auto& a, auto b){if (a < b) {a = b; return true;} return false;};
void debug_impl() { std::cerr << std::endl; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail){
  std::cerr << " " << head;
  debug_impl(tail...);
}
#ifndef STOPIT
#define debug(...)\
  std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:";\
  debug_impl(__VA_ARGS__);\
  std::cerr << std::noboolalpha;
#else
#define debug 0;
#endif

template < typename Container, typename Value = typename Container::value_type, std::enable_if_t<!std::is_same< Container, std::string >::value, std::nullptr_t> = nullptr>
std::istream& operator>> (std::istream& is, Container& v)
  { for (auto & x : v) { is >> x; } return is; }

template < typename Container, typename Value = typename Container::value_type, std::enable_if_t<!std::is_same< Container, std::string >::value, std::nullptr_t> = nullptr >
std::ostream& operator<< (std::ostream& os, Container const& v) {
 os << "{";
  for (auto it = v.begin(); it != v.end(); it++)
    {os << (it != v.begin() ? "," : "") << *it;}
  return os << "}";
}

template < template < typename ... > class Tuple,  typename... Args, std::size_t ... Inds, std::size_t = std::tuple_size< Tuple < Args ... > >::value >
std::istream& tuple_input_impl(std::istream& os, Tuple<Args...>& tuple, std::integer_sequence<std::size_t, Inds...>)
  { (void)std::initializer_list<int>{((void)(os >> std::get< Inds >(tuple)), 0)...}; return os; }
template < template < typename ... > class Tuple,  typename... Args, std::size_t = std::tuple_size< Tuple < Args ... > >::value >
std::istream& operator>> (std::istream& os, Tuple<Args...>& tuple)
  { return tuple_input_impl(os, tuple, std::index_sequence_for<Args...>()); }

template < template < typename ... > class Tuple,  typename... Args, std::size_t ... Inds, std::size_t = std::tuple_size< Tuple < Args ... > >::value >
std::ostream& tuple_output_impl(std::ostream& os, const Tuple<Args...>& tuple, std::integer_sequence<std::size_t, Inds...>)
  { os << "("; (void)std::initializer_list<int>{((void)(os << (Inds > 0 ? "," : "") << std::get< Inds >(tuple)), 0)...}; return os << ")"; }
template < template < typename ... > class Tuple,  typename... Args, std::size_t = std::tuple_size< Tuple < Args ... > >::value >
std::ostream& operator<< (std::ostream& os, const Tuple<Args...>& tuple)
 { return tuple_output_impl(os, tuple, std::index_sequence_for<Args...>()); }

template < template < typename ... > class Container, typename Value >
auto runlength(Container< Value > const& v) {
  auto n = (int)v.size();
  if (n == 0)
    { return Container< std::pair< Value, int > >{}; }
  auto pos = Container< int >{};
  for (auto i = 1; i < n; i++) {
    if (v.at(i - 1) != v.at(i))
      { pos.emplace_back(i); }
  }
  pos.emplace_back(n);
  auto m = (int)pos.size();
  auto len = Container< int >(m);
  std::adjacent_difference(pos.begin(), pos.end(), len.begin());
  auto ret = Container< std::pair< Value, int > >(m);
  for (auto i = 0; i != m; i++)
    { ret.at(i) = {v.at(pos.at(i) - 1), len.at(i)}; }
  return ret;
}

int main() {
  std::cin.tie(0); std::cin.sync_with_stdio(false);
  std::string s; std::cin >> s;
  lint n = s.length();
  lint k; std::cin >> k;
  auto a = std::vector< char >(s.begin(), s.end());
  auto rle = runlength(a);
  // debug(rle);
  auto cal = [&] (auto x)
    { return x / 2; };

  if (rle.size() == 1u) {
    auto ret = n * k / 2;
    std::cout << ret << std::endl;
    return 0;
  }

  auto x = rle.front().second;
  auto y = rle.back().second;
  lint ret = 0;
  if (rle.front().first == rle.back().first) {
    ret += cal(x) + cal(y) + cal(x + y) * (k - 1);
  }
  else {
    ret += cal(x) * k + cal(y) * k;
  }

  auto l = rle.size();
  rep(i, 1, l - 1) {
    auto x = rle.at(i).second;
    ret += cal(x) * k;
  }
  std::cout << ret << std::endl;
  return 0;
}


Measured memory_bytes: 256