#include <cstdio>
#include <cstring>
#include <algorithm>

int f[1000005][2];
char s[1000005];

int main() {
    scanf("%s", s);
    int n = strlen(s);
    f[n + 1][1] = 100000000;
    for (int i = n; i; --i) {
        int x = s[i - 1] - 48;
        f[i][0] = std::min(f[i + 1][0], f[i + 1][1] + 1) + x;
        f[i][1] = std::min(f[i + 1][0], f[i + 1][1] - 1) + 10 - x;
    }
    printf("%d\n", std::min(f[1][0], f[1][1] + 1));
    return 0;
}
