// Arup Guha
// 3/6/2021
// Solution to 2020 SER Problem: Ant Typing

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int go(int perm[], int used[], int k);
int eval(int perm[]);
int min(int a, int b);
int max(int a, int b);

// Easier to make these global.
int trans[9][9];
int n;
char* list;

int main(void) {

    // Get input.
    list = malloc(sizeof(char)*100001);
    scanf("%s", list);

    // Store #of each transition.
    for (int i=0; i<81; i++) trans[i/9][i%9] = 0;

    n = strlen(list);

    // Go through each transition.
    for (int i=0; i<n-1; i++) {
        int a = list[i] - '1';
        int b = list[i+1] - '1';
        trans[min(a,b)][max(a,b)]++;
    }

    // Set up permutation solution.
    int perm[9];
    int used[9];
    for (int i=0; i<9; i++) used[i] = 0;

    // Ta da!
    printf("%d\n", go(perm, used, 0));

    free(list);
    return 0;
}

// Returns the best answer for fixing the first k items in perm.
int go(int perm[], int used[], int k) {

    // Finished filling in this permutation.
    if (k == 9) return eval(perm);

    int res = 1000000000;

    // Try each item in slot k, storing the lowest answer.
    for (int i=0; i<9; i++) {
        if (used[i]) continue;
        perm[k] = i;
        used[i] = 1;
        int tmp = go(perm, used, k+1);
        if (tmp < res) res = tmp;
        used[i] = 0;
    }

    // Ta da!
    return res;
}

// Get the result for this permutation.
int eval(int perm[]) {

    // Store where each key is.
    int where[9];
    for (int i=0; i<9; i++) where[perm[i]] = i;

    // Init cost is for pressing all the keys.
    int cost = n;

    // Add in cost for getting to first key.
    cost += abs(where[perm[0]] - where[list[0]-'1']);

    // Now go through each transition.
    for (int i=0; i<9; i++)
        for (int j=i+1; j<9; j++)
            cost += abs(where[i]-where[j])*trans[i][j];

    // This is the total cost.
    return cost;
}

int min(int a, int b) {
    return a < b ? a : b;
}

int max(int a, int b) {
    return a > b ? a : b;
}
