#include #include #define SMAX 10001 #define INF 1e9 int main (void) { int i, j, k, slen, cost[10] = {0, 1, 2, 3, 4, 1, 2, 3, 4, 5}, dp[2][2] = {0, INF}; char str[SMAX+1]; scanf("%s%*c", str); slen = strlen(str); for (i = 0; i < slen; i++) { int n = str[slen-1-i] - '0'; dp[(i+1)&1][0] = dp[(i+1)&1][1] = INF; for (j = 0; j < 2; j++) { for (k = 0; k <= 9; k++) { if ((k % 5) != 0 && ((n + j + k) % 5) != 0) continue; if (n + j + k <= 9) { if (dp[(i+1)&1][0] > dp[i&1][j] + cost[k] + cost[n+j+k]) { dp[(i+1)&1][0] = dp[i&1][j] + cost[k] + cost[n+j+k]; } } else { if (dp[(i+1)&1][1] > dp[i&1][j] + cost[k] + cost[n+j+k-10]) { dp[(i+1)&1][1] = dp[i&1][j] + cost[k] + cost[n+j+k-10]; } } } } } if (dp[slen&1][0] < dp[slen&1][1] + 1) printf("%d\n", dp[slen&1][0]); else printf("%d\n", dp[slen&1][1] + 1); }