Problem:
Score : 300 points

Problem Statement

Snuke can change a string t of length N into a string t' of length N - 1 under the following rule:

For each i (1 ≤ i ≤ N - 1), the i-th character of t' must be either the i-th or (i + 1)-th character of t.

There is a string s consisting of lowercase English letters.
Snuke's objective is to apply the above operation to s repeatedly so that all the characters in s are the same.
Find the minimum necessary number of operations.

Constraints

1 ≤ |s| ≤ 100

s consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s

Output

Print the minimum necessary number of operations to achieve the objective.

Sample Input 1

serval

Sample Output 1

3

One solution is: serval → srvvl → svvv → vvv.

Sample Input 2

jackal

Sample Output 2

2

One solution is: jackal → aacaa → aaaa.

Sample Input 3

zzz

Sample Output 3

0

All the characters in s are the same from the beginning.

Sample Input 4

whbrjpjyhsrywlqjxdbrbaomnw

Sample Output 4

8

In 8 operations, he can change s to rrrrrrrrrrrrrrrrrr.

Solution in Rust (1.15.1) (judge verdict: Accepted)

#[cfg(test)]
#[macro_use]
extern crate contest;

use std::cmp;
use std::collections::VecDeque;
use std::fmt::Debug;
use std::io::{self, Bytes, Read, Stdin, StdinLock};
use std::str::{self, FromStr};

fn main() {
    let stdin = io::stdin();
    let mut sc = StdinScanner::new(&stdin);
    println!("{}", solve(sc.next()));
}


fn solve(s: String) -> u32 {
    let mut result = 1000;
    let q = {
        let mut q = VecDeque::new();
        for c in s.as_bytes() {
            q.push_back(c);
        }
        q
    };
    for target in 'a' as u8..'z' as u8 + 1 {
        if s.as_bytes().iter().find(|&&c| c == target).is_none() {
            continue;
        }
        let mut n = 0;
        let mut q = q.clone();
        while !q.is_empty() {
            if *q[0] == target {
                q.pop_front();
            } else {
                let l = q.len();
                for i in 0..l - 1 {
                    if *q[i + 1] == target {
                        q[i] = &target;
                    }
                }
                n += 1;
                q.pop_back();
            }
        }
        result = cmp::min(result, n);
    }
    result
}


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

    #[test]
    fn test() {
        assert_eq_in_time!(1000, "", "");
    }
}


struct StdinScanner<'a> {
    buf: Vec<u8>,
    bytes: Bytes<StdinLock<'a>>,
}


#[allow(dead_code)]
impl<'a> StdinScanner<'a> {
    fn new(stdin: &'a Stdin) -> Self {
        StdinScanner {
            buf: Vec::with_capacity(256),
            bytes: stdin.lock().bytes(),
        }
    }

    fn next<T: FromStr>(&mut self) -> T
        where <T as FromStr>::Err: Debug
    {
        self.buf.clear();
        let mut c = self.bytes.next().unwrap().unwrap();
        while c == ' ' as u8 || c == '\n' as u8 {
            c = self.bytes.next().unwrap().unwrap();
        }
        while !(c == ' ' as u8 || c == '\n' as u8) {
            self.buf.push(c);
            c = self.bytes.next().unwrap().unwrap();
        }
        str::from_utf8(&self.buf).unwrap().parse().unwrap()
    }

    fn vec<T: FromStr>(&mut self, n: usize) -> Vec<T>
        where <T as FromStr>::Err: Debug
    {
        (0..n).map(|_| self.next()).collect()
    }

    fn pairs<A: FromStr, B: FromStr>(&mut self, n: usize) -> Vec<(A, B)>
        where <A as FromStr>::Err: Debug,
              <B as FromStr>::Err: Debug
    {
        (0..n).map(|_| (self.next(), self.next())).collect()
    }

    fn trios<A: FromStr, B: FromStr, C: FromStr>(&mut self, n: usize) -> Vec<(A, B, C)>
        where <A as FromStr>::Err: Debug,
              <B as FromStr>::Err: Debug,
              <C as FromStr>::Err: Debug
    {
        (0..n)
            .map(|_| (self.next(), self.next(), self.next()))
            .collect()
    }

    fn mat<T: FromStr>(&mut self, m: usize, n: usize) -> Vec<Vec<T>>
        where <T as FromStr>::Err: Debug
    {
        (0..n).map(|_| self.vec(m)).collect()
    }
}


trait PrintYn {
    fn print_yn(self, yes: &str, no: &str);
}

impl PrintYn for bool {
    fn print_yn(self, yes: &str, no: &str) {
        println!("{}", if self { yes } else { no });
    }
}