#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define all(a) a.begin(), a.end()
#define ff(i,a,b) for(int i=a;i<=b;i++)
#define fb(i,b,a) for(int i=b;i>=a;i--)

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 100005;
const int inf = 1e9 + 5;

int n;
string s;

set<int> se;

int kec[10];
int last[10];

ll bor[4*maxn][10];
void build(int v, int tl, int tr, int k){
    if(tl == tr){
        bor[v][k] = (s[tl] - '0' == k);
        return;
    }
    int mid = (tl + tr)/2;
    build(v*2, tl, mid, k);
    build(v*2+1, mid+1, tr, k);
    bor[v][k] = bor[v*2][k] + bor[v*2+1][k];
}
int kveri(int v, int tl, int tr, int l, int r, int k){
    if(tl > r || l > tr)return 0;
    if(l <= tl && r >= tr)return bor[v][k];
    int mid = (tl + tr)/2;
    return kveri(v*2,tl,mid,l,r,k) + kveri(v*2+1,mid+1,tr,l,r,k);
}
void update(int v, int tl, int tr, int poz, int vre, int k){
    if(tl == tr){
        bor[v][k] += vre;
        return;
    }
    int mid = (tl+tr)/2;
    if(poz <= mid)update(v*2,tl,mid,poz,vre,k);
    else update(v*2+1,mid+1,tr,poz,vre,k);
    bor[v][k] = bor[v*2][k] + bor[v*2+1][k];
}

int prefa[maxn];
int prefb[maxn];
void sub_task2(){
    int a = *se.begin();
    int b = *se.rbegin();
//    cout << "a: " << a << endl;
//    cout << "b: " << b << endl;
    vector<pii> vec;
    ff(i,1,n){
        int x = s[i] - '0';
        int j = i;
        while(j + 1 <= n && x == (s[j + 1] - '0'))j++;
        vec.pb({i, j});
        i = j;
    }
    int kola = 0;
    int kolb = 0;
    ff(i,1,n){
        kola += (s[i] - '0' == a);
        kolb += (s[i] - '0' == b);
    }
    int rez = min(n - kola, n - kolb);
    ff(i,1,n){
        prefb[i] = prefb[i - 1] + (s[i] - '0' == b);
        prefa[i] = prefa[i - 1] + (s[i] - '0' == a);
        rez = min(rez, prefb[i] + kola - prefa[i]);
        rez = min(rez, prefa[i] + kolb - prefb[i]);
    }
    cout << rez << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cout.tie(nullptr);
    cin.tie(nullptr);
    cin >> s;
    n = s.size();
    s = ' ' + s;
    ff(i,1,n){
        int x = s[i] - '0';
        if(!kec[x])kec[x] = i;
        last[x] = i;
        se.insert(x);
    }
    if(se.size() == 1)return cout << 0, 0;
    if(se.size() == 2){
        sub_task2();
        return 0;
    }
    ff(i,0,9)build(1,1,n,i);
    int rez = 0;
    int j = 1;
    while(j <= n){
        int l = j;
        int r = last[s[j] - '0'];
//        cout << "range: " << l << " " << r << endl;
        int najm = inf;
        int koji = 0;
        ff(i,0,9){
//            najm = min(najm, zbir - (pref[r][i] - pref[l - 1][i]));
            int kol = r - l + 1 - kveri(1,1,n,l,r,i);
            if(kol < najm){
                najm = kol;
                koji = i;
            }
            if(kol == najm){
                if(koji == s[j] - '0' || i == s[j] - '0')koji = s[j] - '0';
            }
        }
//        cout << "najm: " << najm << endl;
        rez += najm - (koji != s[j] - '0');
//        cout << "koji: " << koji << endl;
//        cout << najm - (koji != s[j] - '0') << endl;
        if(koji == s[j] - '0')j = r + 1;
        else{
            update(1,1,n,r,-1,s[j] - '0');
            s[r] = char(koji + '0');
            update(1,1,n,r,1,koji);
            j = r;
        }
//        cout << "j: " << j << endl;
        if(j == n)break;
    }
    cout << rez << endl;
    return 0;
}
/**
0112102222221122

11112222122221122222112222

11112222122221122222112222333333333222

01121022111122

0000111100110
**/