#include <iostream>
#include <string>
using namespace std;

void cvrst(int a[3][9], string s, bool c)
{
    for (int i = 0; i <= 9; i++)
    {
        string x = to_string(i);
        a[0][i] = s.find_first_of(x);
        a[1][i] = s.find_last_of(x);
        for (int j = a[0][i]; j < a[1][i]; j++)
        {
            string w = s.substr(j, 1);
            if (w != x)
                a[2][i] = 1;
            c = false;
            break;
        }
    }
}
int main()
{
    string s;
    cin >> s;
    string save_s = s;
    int br = s.length();
    int vel = br;
    int a[3][9];
    int save[3][9];
    bool c = true;
    cvrst(a, s,c);
    if (c) 
        cout << 0;
    else
    {
        for (int i = 0; i <= 9; i++)
        {
            int temp_br = 0;
            string x = to_string(i);
            a[0][i] = s.find_first_of(x);
            a[1][i] = s.find_last_of(x);

            if (a[2][i] == 1)
            {
                string pre = s.substr(0, a[0][i]);
                string posle = s.substr(a[1][i], vel - a[1][i]);
                string izmedju = "";
                for (int z = a[0][i]; z < a[1][i]; z++)
                {
                    izmedju += to_string(i);
                    temp_br++;
                }
                s = pre + izmedju + posle;
            }
            cvrst(a, s, c);
            if (cvrst)
            {
                if (temp_br < br)
                    br = temp_br;
            }
            s = save_s;
        }
        cout << br << endl;
    }
    
}