#include <iostream>
#include <bits/stdc++.h>
using namespace std;
string N;
int br[9],rez=0;
bool u=false,en=true;
int main()
{
    cin>>N;
    for(int i=0;i<N.size();i++){
        br[N[i]-'0']++;
    }
    for(int i=0;i<10;i++){
        if(br[i]>1)en=false;
    }


    string a,b="",c="";
    int ba=0,mi;
    a=N[0];
    if(en)cout<<0;
    else{
        for(int i=1;i<N.size();i++){

            if(b==""&&a[0]==N[i]){a+=N[i];}
            if(a.size()==br[a[0]-'0']){u=true;}

            else if(a[0]!=N[i]){if(b=="")ba+=a.size()-1;b+=N[i];}
            else if(a[0]==N[i]){
                c+=N[i];cout<<" c "<<c;
            }
            else{

                    if(a.size()<=b.size()&&a.size()<=b.size()){rez+=a.size();br[a[0]-'0']-=a.size();u=true;}
                    else if(b.size()<=a.size()&&b.size()<=c.size()){rez+=b.size();for(int h=i;h<b.size();h++)br[b[h]-'0']--;ba+=b.size()-2+c.size();c="";b="";}
                    else {rez+=c.size();br[a[0]-'0']-=c.size();if(a.size()==br[a[0]-'0']) u=true; else{b+=c;c="";}}
            }

            if(u){a=N[i];i=ba;u=false;c="";b="";}


        }
    cout<<rez;

    }

}