#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) { auto cc = [](int x) { return x % 10 / 5 + x % 5; }; for (int d = 0; d < 20; ++d) { if (N[i] - '0' <= d && d - (N[i] - '0') < 10) { dp[i + 1][0] = min(dp[i + 1][0], dp[i][10 <= d] + cc(d) + cc(d - (N[i] - '0'))); } if (N[i] - '0' < d && d - (N[i] - '0') < 10) { dp[i + 1][1] = min(dp[i + 1][1], dp[i][10 <= d] + cc(d) + cc(d - (N[i] - '0') - 1)); } } } cout << dp[N.size()][0] << endl; return 0; }