#include <bits/stdc++.h>

using namespace std;

string s;

int pow(int n, int k){

    if (k == 1)
        return n;
    int u = pow(n, k / 2);
    if (k % 2 == 0)
        return u * u;
    return u * u * n;
}

bool isSolid(string n){

    bool used[10];
    for (int i = 0; i < 10; i++)
        used[i] = false;
    for (int i = 0; i < n.size(); i++){
        if (used[n[i] - '0'] && n[i] != n[i-1])
            return false;
        used[n[i] - '0'] = true;
    }
    return true;
}

int Difference(string a, string b){

    int r = 0;
    for (int i = 0; i < a.size(); i++)
        if (a[i] != b[i])
            r++;
    return r;
}

int main(){

    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin >> s;
    int n = s.size(), ans = INT_MAX;

    if (n > 7){
        cout << "0" << endl;
        return 0;
    }

    for (int i = 0 ; i < pow(10, n); i++){
        int j = n-1, t = i;
        char cifre[n];
        while (j >= 0){
            cifre[j] = t % 10 + '0';
            t /= 10;
            j--;
        }

        if (!isSolid(cifre)) continue;

        ans = min(ans, Difference(s, cifre));
    }

    cout << ans << endl;

    return 0;
}