#include <bits/stdc++.h>
#define ll long long

using namespace std;

int N;
bool cifre[10][6];
int ji;
int najmanjibr = INT_MAX;
int br;
int granica;
int poz;
bool cifra [10];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> N;
	if (N == 1000000)
    {
        cout << 0;
        return 0;
    }
	ji = 5;
	while (N != 0)
    {
        int c = N % 10;
        cifra [c] = true;
        if (c > granica)
            granica = c;
        cifre [c] [ji] = true;
        N /= 10;
        ji--;
    }
    for (int i = 0; i <= granica; i++)
    {
        for (int j = 0; j < 6; j++)
        {
            if (cifre[i][j])
                poz = j;
            else if ((!cifre[i][j]) && (j > ji) && (cifra[i]))
                br++;
        }
        br -= 5 - poz;
        if ((br < najmanjibr) && (cifra[i]))
            najmanjibr = br;
        br = 0;
    }
    cout << najmanjibr;
	return 0;
}