#include #include #include #include #include #include #include #include #include #include #include #define rep(i,n) for(int i = 0; i < n; ++i) #define rep1(i,n) for(int i = 1; i <= n; ++i) using namespace std; templatebool chmax(T &a, const T &b) { if(a < b){ a = b; return 1; } return 0; } templatebool chmin(T &a, const T &b) { if(a > b){ a = b; return 1; } return 0; } template inline int sz(T &a) { return a.size(); } using ll = long long; using ld = long double; using pi = pair; using pl = pair; using vi = vector; using vvi = vector; using vl = vector; using vvl = vector; const int inf = numeric_limits::max(); const ll infll = numeric_limits::max(); int main() { string s; cin >> s; int n = sz(s); vvl dp1(2, vl(2, infll/2)); dp1[0][0] = 0; dp1[1][0] = 1; rep(i,n) { vvl dp2(2, vl(2, infll/2)); int c = int(s[i] - '0'); rep(j,10) { if(c < j) { if(c+1 < j) chmin(dp2[1][1], dp1[0][0] + j/5 + j%5 + (j-c-1)/5 + (j-c-1)%5); chmin(dp2[1][1], dp1[1][0] + j/5 + j%5 + (j-c-1)/5 + (j-c-1)%5); chmin(dp2[1][0], dp1[1][0] + j/5 + j%5 + (j-c)/5 + (j-c)%5); chmin(dp2[1][0], dp1[0][0] + j/5 + j%5 + (j-c)/5 + (j-c)%5); } if(c == j) { chmin(dp2[1][1], dp1[1][0] + j/5 + j%5 + (9)/5 + (9)%5); chmin(dp2[1][0], dp1[1][0] + j/5 + j%5); chmin(dp2[0][0], dp1[0][0] + j/5 + j%5); } if(c > j) { chmin(dp2[1][1], dp1[1][0] + j/5 + j%5 + (9 + j - c)/5 + (9 + j - c)%5); chmin(dp2[1][1], dp1[1][1] + j/5 + j%5 + (9 + j - c)/5 + (9 + j - c)%5); chmin(dp2[1][0], dp1[1][0] + j/5 + j%5 + (10 + j - c)/5 + (10 + j - c)%5); chmin(dp2[1][0], dp1[1][1] + j/5 + j%5 + (10 + j - c)/5 + (10 + j - c)%5); } } swap(dp1, dp2); // cout << i << ":" << dp1[0][0] << ":" << dp1[1][0] << ";" << dp1[0][1] << ":" << dp1[1][1] << "\n"; } cout << min(dp1[0][0], dp1[1][0]) << "\n"; return 0; }