#include using namespace std; int main() { ios::sync_with_stdio(false); string N; cin >> N; vector> dp(N.size() + 1, vector(2, 0x3f3f3f3f)); dp[0][0] = 0; dp[0][1] = 1; for (int i = 0; i < N.size(); ++i) { int cs[] = { 0, 1, 5 }; auto cc = [&cs](int x) { assert(-1 < x && x < 11); return x % 10 / 5 + x % 5; }; for (int d = 0; d < 11; ++d) { if (N[i] - '0' <= d) { dp[i + 1][0] = min(dp[i + 1][0], dp[i][d == 10] + cc(d) + cc(d - (N[i] - '0'))); } if (N[i] - '0' < d) { dp[i + 1][1] = min(dp[i + 1][1], dp[i][d == 10] + cc(d) + cc(d - (N[i] - '0') - 1)); } } } cout << dp[N.size()][0] << endl; return 0; }