Problem:
Score : 300 points

Problem Statement

Given is a string S. Let T be the concatenation of K copies of S.
We can repeatedly perform the following operation: choose a character in T and replace it with a different character.
Find the minimum number of operations required to satisfy the following condition: any two adjacent characters in T are different.

Constraints

1 \leq |S| \leq 100

S consists of lowercase English letters.

1 \leq K \leq 10^9

K is an integer.

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the minimum number of operations required.

Sample Input 1

issii
2

Sample Output 1

4

T is issiiissii. For example, we can rewrite it into ispiqisyhi, and now any two adjacent characters are different.

Sample Input 2

qq
81

Sample Output 2

81

Sample Input 3

cooooooooonteeeeeeeeeest
999993333

Sample Output 3

8999939997

Solution in Rust (1.15.1) (judge verdict: Wrong Answer)

use std::cmp;

fn main() {
    let s = std::io::stdin();
    let mut sc = Scanner { stdin: s.lock() };
    let mut s = sc.chars();
    let n = s.len();
    let k: usize = sc.read();

    if s[0] != s[n - 1] {
        let ans = calc(&mut s);
        println!("{}", ans * k);
        return;
    }

    let mut s1 = s.clone();
    let ans1 = calc(&mut s1);
    if s1[n - 1] == '.' {
        println!("{}", ans1 * k);
        return;
    }

    let ans_t = ans1 * k + k - 1;

    let mut s2 = s.clone();
    s2[0] = '.';
    let ans2 = calc(&mut s2);
    if s2[n - 1] == '.' {
        let ans = (k + 1) / 2 * ans1 + k / 2 * (ans2 + 1);
        println!("{}", cmp::min(ans, ans_t));
        return;
    }
    // s1: a...a
    // s2: x...a

    let ans = (k + 1) / 2 * ans1 + k / 2 * (ans2 + 2);
    println!("{}", cmp::min(ans, ans_t));
}

fn calc(s: &mut Vec<char>) -> usize {
    let n = s.len();
    let mut ans = 0;
    for i in 0..(n - 1) {
        if s[i] == s[i + 1] {
            s[i + 1] = '.';
            ans += 1;
        }
    }
    ans
}

pub struct Scanner<R> {
    stdin: R,
}

impl<R: std::io::Read> Scanner<R> {
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .stdin
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n')
            .take_while(|&b| b != b' ' && b != b'\n')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}