#include <bits/stdc++.h>

using namespace std;
struct Border{
    int BorderLocation,BorderWeakness; //BorderLoc=0 |1,2,3,4 ;; BorderLoc=3 1,2,3|4
    vector<int> Infront;

};
string s;
int n;
int border=0;
vector<bool> disregard(10,true);
vector<int> InfrontBorder(10,0);
vector<int> BehindBorder(10,0);

Border BestBorderForK(int digit){
    Border b;
    b.BorderWeakness=InfrontBorder[digit];
    b.Infront=InfrontBorder;
    b.BorderLocation=border;
    vector<int> Infront=InfrontBorder;
    int behind=0;
    for(int i=border;i<n;i++){
        int d=s[i]-'0';
        if(!disregard[d]){

            if(d!=digit)
                behind++;
            Infront[d]--;

        }
        if(b.BorderWeakness>=behind+Infront[digit]){
            b.BorderWeakness=behind+Infront[digit];
            b.Infront=Infront;
            b.BorderLocation=i+1;
        }
    }
    return b;
}

int main()
{

    cin>>s;
    n=s.size();

    for(int i=0;i<n;i++){
        InfrontBorder[s[i]-'0']++;
        disregard[s[i]-'0']=false;
    }
    int res=0;

    while(border!=n){
        Border naj;
        naj.BorderWeakness=-1;
        int x;
        for(int k=0;k<10;k++){
            if(disregard[k])continue;
            Border b=BestBorderForK(k);
            if((naj.BorderWeakness>b.BorderWeakness&&b.BorderLocation!=border)||naj.BorderWeakness==-1)
                {naj=b;x=k;}
        }
        border=naj.BorderLocation;
        InfrontBorder=naj.Infront;
        res+=naj.BorderWeakness;
        disregard[x]=true;
        //cout<<x<<" "<<border<<" "<<res<<endl;
    }

    cout << res << endl;
    return 0;
}
/*
#define fi first
#define se second
using namespace std;

int n;

int main()
{
    string s;
    cin>>s;
    n=s.size();
    pair<int,int> n1=make_pair(0,0),n0=make_pair(0,0);
    for(int i=0;i<n;i++){
        if(s[i]=='0')n0.se++;
        else n1.se++;
    }
    int res=min(n1.se,n0.se);
    for(int i=0;i<n;i++){
        if(s[i]=='0'){n0.se--;n0.fi++;}
        else {n1.se--;n1.fi++;}
        res=min(res,min(n0.se+n1.first,n1.se+n0.first));
    }
    cout << res << endl;
    return 0;
}
*/