#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <map>
#include <set>
#define forn(i, n) for (int i = 0; i < n; i++)
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    string s;
    cin >> s;
    int n = s.size();
    int a[n];
    forn(i, n) a[i] = s[i]-'0';
    set<int> nums = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    int minans = INT_MAX;
    int currentcost = 0;
    for (int i = 0; i < n && nums.size() > 0;) {
        int oof[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        pair<int, int> maxes[n-i];
        for (int j = i; j < n; j++) {
            for (int l = 0; l < 10; l++) oof[l]+=(a[j]==l)?1:-1;
            // maxid
            int minid = 0;
            for (int l = 1; l < 10; l++) 
                if (nums.count(l) && (!nums.count(minid) || oof[l] >= oof[minid])) minid = l;
            maxes[j-i] = {minid, oof[minid]};
        }
        int mincuga = INT_MIN;
        int mincugaid = -1;
        forn(j, n-i) if (nums.count(maxes[j].first) && maxes[j].second >= mincuga) {
            mincuga = maxes[j].second;
            mincugaid = j+i;
        }
        nums.erase(maxes[mincugaid-i].first);
        for (int j = i; j <= mincugaid; j++) currentcost+=(a[j]==maxes[mincugaid-i].first)?0:1;
        int cringe = 0;
        for (int j = mincugaid+1; j < n; j++) cringe+=(a[j]==maxes[mincugaid-i].first)?0:1;
        minans=min(minans, currentcost+cringe);
        i+=1+mincugaid-i;
    }
    cout << minans << endl;
    #ifdef CHUNGUS
    system("pause");
    #endif
}