#include <iostream>
#include <unordered_map>
#include <vector>
#include <queue>

bool check(int m1, int m2, int a, int b) {
    if (a > m1 && a < m2) {
        return true;
    }
    
    if (b > m1 && b < m2) {
        return true;
    }
    
    return false;
}

using namespace std;

int main() {
    string s;
    char c;
    
    int max = INT_MIN, m1 = -1, m2 = -1, res;
    char cx = 'x';
    
    priority_queue<int, vector<int>, greater<int> > p;
    
    cin >> s;
    
    unordered_map<char, pair<int, int> > m;
    
    vector<tuple<char, int, int> > v;
    
    for (int i = 0; i < s.length(); ++i) {
        c = s[i];
        
        if (m.find(c) == m.end()) {
            m[c] = make_pair(i, -1);
        } else if (i > m[c].second) {
            m[c].second = i;
        }
    }
        
    for (auto it = m.begin(); it != m.end(); ++it) {
        if (it->second.second - it->second.first >= max) {
            m1 = it->second.first;
            m2 = it->second.second;
            cx = it->first;
                
            max = m2 - m1;
        }
    }
        
    for (auto it = m.begin(); it != m.end(); ++it) {
        if (it->second.second - it->second.first == max && !check(m1, m2, it->second.first, it->second.second)) {
            v.emplace_back(make_tuple(it->first, it->second.first, it->second.second));
        }
    }
    
    for (auto i : v) {
        res = 0;
        
        for (int j = get<1>(i) + 1; j < get<2>(i); ++j) {
            if (s[j] != get<0>(i)) {
                ++res;
            }
        }
        
        p.push(res);
    }
    
    if (p.size()) {
        cout << p.top();
    } else {
        cout << 0;
    }
}