#include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define REP(i,n) for(int i=0; i=b; --i) #define ALL(c) (c).begin(), (c).end() typedef long long ll; typedef vector VI; typedef vector VL; typedef vector VVL; typedef vector VVI; typedef pair P; typedef pair PL; const int N = 10010; const int INF = 1e9; int dp[N][2]; int cost(int x){ if (x < 5) return x; return x - 4; } int main() { string s; cin >> s; s = '0' + s; reverse(ALL(s)); int n = s.length(); REP(i,n+1) dp[i][0] = dp[i][1] = INF; dp[0][0] = 0; REP(i,n) REP(j,2){ int x = s[i] - '0'; REP(k,10){ int d = k - x - j; if (d >= 0){ dp[i+1][0] = min(dp[i+1][0], dp[i][j] + cost(k) + cost(d)); }else{ dp[i+1][1] = min(dp[i+1][1], dp[i][j] + cost(k) + cost((d + 10) % 10)); } } } // REP(i,n+1){ // cout << dp[i][0] << " " << dp[i][1] << endl; // } cout << dp[n][0] << endl; return 0; }