#include <bits/stdc++.h>
using namespace std;

int main()
{
    string n;
    cin >> n;
    map<char, int> mp;
    map<char, int> mp_end;
    for (int i = 0; i < n.size(); i++) {
        mp[n[i]]++;
        mp_end[n[i]] = i;
    }
    vector<char> cif;
    int mmax = -1;
    for (map<char, int>::iterator it = mp.begin(); it != mp.end(); it++) {
        mmax = max(mmax, it -> second);
    }
    for (map<char, int>::iterator it = mp.begin(); it != mp.end(); it++) {
        if (it -> second == mmax) cif.push_back(it -> first);
    }
    int ans = n.size();
    for (int x = 0; x < cif.size(); x++) {
        char c = cif[x];
        int cnt = 0;
        for (int i = 0; i < n.size(); i++) {
            if (n[i] == c) {
                int j = i + 1;
                while(n[j] != c && j <= mp_end[c]) {
                    mp[n[j]]--;
                    j++;
                }
                cnt += j - i - 1;
                i = j;
            }
        }
        int i = 0;
        while(n[i] != c) {
            cnt++;
            i++;
        }
        ans = min(ans, cnt);
    }
    cout << ans << "\n";
    return 0;
}