//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 300005
bool valid(){
    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[ans[i]-'0'][0]=min(a[ans[i]-'0'][0],i);
        a[ans[i]-'0'][1]=max(a[ans[i]-'0'][1],i);
        a[ans[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;
void solve(int i){
    if(i==n){
        //cout<<ans<<" ";
        if(valid()){
            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 v[10];
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;
    }
    for(int i = 0;i<10;i++) car.insert('0'+i);
    solve(0);
    cout<<rez;
    /*cout<<"\n";
    vremeend
    vremeispis*/
    return 0;
}