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: Accepted)

use std::io::{self, BufRead, BufWriter, Write};

fn solve<R, W>(reader: R, mut writer: W)
where
    R: BufRead,
    W: Write,
{
    let mut lines = reader.lines();
    let s: Vec<char> = lines.next().unwrap().unwrap().chars().collect();
    let k: i64 = lines.next().unwrap().unwrap().parse().unwrap();

    let (prefix, middle, suffix) = split_string(&s);

    let ans =
        if prefix.len() == s.len() {
            // same string
            let len = s.len() as i64 * k;
            if len % 2 == 0 { len / 2 } else { (len - 1) / 2 }
        } else if s[0] == s[s.len() - 1] {
            let len0 = prefix.len() as i64;
            let len0 = if len0 % 2 == 0 { len0 / 2 } else { (len0 - 1) / 2 };
            let len1 = suffix.len() as i64;
            let len1 = if len1 % 2 == 0 { len1 / 2 } else { (len1 - 1) / 2 };
            let len_middle = prefix.len() as i64 + suffix.len() as i64;
            let len_middle = if len_middle % 2 == 0 { len_middle / 2 } else { (len_middle - 1) / 2 };
            min_tr(middle) * k + len_middle * (k - 1) + len0 + len1
        } else {
            min_tr(&s) * k
        };


    writeln!(&mut writer, "{}", ans).unwrap();
}

fn min_tr(s: &[char]) -> i64 {
    let mut ret: i64 = 0;
    let mut prev = '*';
    let mut len: i64 = 0;

    for &c in s {
        if c != prev {
            ret += if len % 2 == 0 { len / 2 } else { (len - 1) / 2 };
            len = 0;
        }

        len += 1;
        prev = c;
    }

    if len > 0 {
        ret += if len % 2 == 0 { len / 2 } else { (len - 1) / 2 };
    }

    ret
}

fn split_string(s: &[char]) -> (&[char], &[char], &[char]) {
    let mut prefix_len = 0;
    for i in 0..s.len() {
        if s[i] != s[0] {
            break;
        }
        prefix_len += 1;
    }

    if prefix_len == s.len() {
        return (s, &[], &[])
    }

    let mut suffix_len = 0;
    for i in 0..s.len() {
        if s[s.len() - i - 1] != s[s.len() - 1] {
            break;
        }
        suffix_len += 1;
    }

    (
        &s[0..prefix_len],
        &s[prefix_len..s.len() - suffix_len],
        &s[s.len() - suffix_len..s.len()],
    )
}

fn main() {
    let stdio = io::stdin();
    let input = stdio.lock();

    let stdout = io::stdout();
    let output = BufWriter::new(stdout.lock());

    solve(input, output);
}

#[cfg(test)]
mod tests {
    use super::*;

    const TESTS: &'static [(&'static [u8], &'static [u8])] = &[
        (
            br"issii
2", br"4
",
        ),
        (
            br"qq
81", br"81
",
        ),
        (
            br"cooooooooonteeeeeeeeeest
999993333", br"8999939997
",
        ),
        (
            br"i
1", br"0
",
        ),
        (
            br"i
3", br"1
",
        ),
        (
            br"aaabbbaaa
3", br"11
",
        ),
    ];

    #[test]
    fn test_a() {
        for &(input, expected) in TESTS {
            let mut output = Vec::new();
            solve(input, &mut output);
            let expected_str = String::from_utf8(expected.to_vec()).unwrap();
            let output_str = String::from_utf8(output).unwrap();
            assert_eq!(expected_str, output_str);
        }
    }
}