//sat
#include <chrono>
using namespace std::chrono;
#define vremestart auto start = high_resolution_clock::now();
#define vremeend auto stop = high_resolution_clock::now();
#define vremeispis auto duration = duration_cast<microseconds>(stop - start); cout << duration.count() << endl;
//sat
#include <bits/stdc++.h>
#define mod 998244353
#define gcd __gcd
#define ll long long
#define ull unsigned long long
#define inf 1e15
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define pii pair<int,int>
#define sz(a) int(a.size())
#define all(a) a.begin(),a.end()
#define rall(a) a.begin(),a.end(),greater<int>()
using namespace std;
string s;
set<char> car;
int n;
string ans = "";
int a[10][3];

#define maxn 100005
bool bio[maxn];
bool valid(string e){
    for(int i = 0;i<10;i++){
        a[i][0] = maxn;
        a[i][1]  = 0;
        a[i][2]  = 0;
    }
    for(int i = 0;i<n;i++){
        a[e[i]-'0'][0]=min(a[e[i]-'0'][0],i);
        a[e[i]-'0'][1]=max(a[e[i]-'0'][1],i);
        a[e[i]-'0'][2]++;
    }
    for(int i = 0;i<10;i++){
        if(a[i][2]==0) continue;
        //cout<<a[i][0]<<" "<<a[i][1]<< " "<<a[i][2]<<"\n";
        if(a[i][1]-a[i][0]+1!=a[i][2]) return 0;
    }
    //cout<<ans<<" ";
    return 1;
}
int rez = maxn;
bool cmp1(vector<int> a,vector<int> b){
    return a[1]-a[0]+1-a[2]<b[1]-b[0]+1-b[2];
}
bool cmp2(pair<vector<int>,pii> a,pair<vector<int>,pii> b){
    if((a.fi).empty()) return 0;
    if((b.fi).empty()) return 1;
    return (a.fi).back()-(a.fi)[0]+1-(a.sc).fi<(b.fi).back()-(b.fi[0])+1-(b.sc).fi;
}
void solve(int i){
    if(i==n){
        //cout<<ans<<" ";
        if(valid(ans)){
            int sum = 0;
            for(int i = 0;i<n;i++){
                if(s[i]!=ans[i]) sum++;
            }
            rez = min(sum,rez);
        }
        //cout<<"\n";
        return;
    }
    for(char slovo : car){
        ans.pb(slovo);
        solve(i+1);
        ans.popb();
    }
    return;
}
int solve2(){
    fill(bio,bio+n,0);
    string ans2 = s;
    vector<pair<vector<int>,pii> > arr(10);
    for(int i = 0;i<10;i++) (arr[i].sc).sc = i;
    for(int i = 0;i<n;i++){
        (arr[s[i]-'0'].fi).pb(i);
        (arr[s[i]-'0'].sc).fi++;
    }
    sort(all(arr),cmp2);
    //for(int i = 0;i<10;i++) cerr<<arr[i][0]<< " "<<arr[i][1]<<" "<<arr[i][2]<< " "<<arr[i][3]<<"\n";
    rez = 0;

    for(int j = 0;j<10;j++){
        if((arr[0].sc).fi==(arr[0].fi).back()-(arr[0].fi)[0]+1){
            (arr[0].sc).fi-=2*maxn;
            sort(all(arr),cmp2);
            continue;
        }else if((arr[0].fi).empty()){
            (arr[0].sc).fi-=2*maxn;
            sort(all(arr),cmp2);
            continue;
        }
        //cerr<<(arr[0].fi)[0]<< " "<<(arr[0].fi).back()<<" "<<(arr[0].sc).fi<<" "<<(arr[0].sc).sc<<"\n";
        for(int i = (arr[0].fi)[0];i<=(arr[0].fi).back();i++){
            if(bio[i]==0&&(arr[0].sc).sc!=s[i]-'0'){
                rez++;
                ans2[i] = (arr[0].sc).sc+'0';
                for(int kk = 0;kk<n;kk++){

                    if((arr[kk].sc).sc==s[i]-'0'){
                        vector<int>::iterator iter = find(all(arr[kk].fi),i);
                        (arr[kk].fi).erase(iter);
                        (arr[kk].sc).fi--;
                        break;
                    }
                }
            }
            bio[i] = 1;
        }
        (arr[0].sc).fi-=2*maxn;
        sort(all(arr),cmp2);
    }
    if(valid(ans2)) return rez;
    else return maxn;
}
int v[10];
int solve1(){
    string ans1 = s;
    fill(bio,bio+n,0);
    vector<vector<int> > arr(10,vector<int>(4,0));
        for(int i = 0;i<10;i++){
        arr[i][0] = maxn;
        arr[i][1]  = 0;
        arr[i][2]  = 0;
        arr[i][3] = i;
    }
    for(int i = 0;i<n;i++){
        arr[s[i]-'0'][0] = min(arr[s[i]-'0'][0],i);
        arr[s[i]-'0'][1] = max(arr[s[i]-'0'][1],i);
        arr[s[i]-'0'][2]++;
    }
    sort(all(arr),cmp1);
    //for(int i = 0;i<10;i++) cerr<<arr[i][0]<< " "<<arr[i][1]<<" "<<arr[i][2]<< " "<<arr[i][3]<<"\n";
    rez = 0;

    for(int j = 0;j<10;j++){
        if(arr[0][2]==arr[0][1]-arr[0][0]+1){
            arr[0][2]-=2*maxn;
            sort(all(arr),cmp1);
            continue;
        }else if(arr[0][0]==maxn){
            arr[0][1] = maxn;
            arr[0][2]-=2*maxn;
            sort(all(arr),cmp1);
            continue;
        }
        //cerr<<arr[0][0]<< " "<<arr[0][1]<<" "<<arr[0][2]<<" "<<arr[0][3]<<"\n";
        for(int i = arr[0][0];i<=arr[0][1];i++){
            if(bio[i]==0&&arr[0][3]!=s[i]-'0'){
                rez++;
                ans1[i] = arr[0][3]+'0';
            }
            bio[i] = 1;
        }
        arr[0][2] = arr[0][1]-arr[0][0]+1;
        arr[0][2]-=2*maxn;
        sort(all(arr),cmp1);
    }
    if(valid(ans1)) return rez;
    else return maxn;
}
int main(){
	ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
    cin >> s;
    //vremestart;
    n = sz(s);
    int c  = 0;
    for(int i = 0;i<n;i++) v[s[i]-'0']++;
    for(int i = 0;i<10;i++){
        if(v[i]>0) c++;
    }
    if(c==2){
        int x = -1,y = -1;
        for(int i = 0;i<10;i++){
            if(v[i]>0){
                if(x==-1) x = i;
                else if(y==-1) y =i;
            }
        }
        int px = 0,sx = v[x],py = 0,sy = v[y];
        int ans = min(sx,sy);
        for(int i = 0;i<n;i++){
            sx-=((s[i]-'0')==x);
            px+=((s[i]-'0')==x);
            sy-=((s[i]-'0')==y);
            py+=((s[i]-'0')==y);
            ans = min(ans,sx+py);
            ans = min(ans,sy+px);
        }
        cout<<ans;
        return 0;
    }
    if(c==1){
        cout<<0;
        return 0;
    }
    if(n<=6){
        for(int i = 0;i<10;i++) car.insert('0'+i);
        solve(0);
        cout<<rez;
        return 0;
    }
    int rezu = min(solve1(),solve2());
    cout<<rezu;
    /*cout<<"\n";
    vremeend
    vremeispis*/
    return 0;
}
/*
input: 1332112
output: 2
*/
/*
input: 222222552222332222
output: 4
*/
/*
input: 32243234
output: 3
*/