#include #include #include #include using namespace std; int a[] = { 0, 1, 2, 3, 2, 1, 2, 3, 3, 2, 1 }; string s; int p[10009], q[10009], dp[10009][2]; int main() { cin >> s; reverse(s.begin(), s.end()); for (int i = 0; i < s.size(); i++) p[i] = s[i] - 48, q[i] = -p[i]; q[s.size()] = 1; while (true) { bool flag = true; for (int i = s.size(); i >= 0; i--) { if (q[i] < 0) flag = false; } if (flag) break; for (int i = s.size() - 1; i >= 0; i--) { if (q[i] < 0) q[i] += 10, q[i + 1]--; } } dp[0][0] = a[q[0]], dp[0][1] = a[p[0]]; for (int i = 1; i < s.size(); i++) { dp[i][0] = min(dp[i - 1][0] + a[q[i]], dp[i - 1][1] + a[q[i] + 1]); dp[i][1] = min(dp[i - 1][0] + a[p[i] + 1], dp[i - 1][1] + a[p[i]]); } cout << min(dp[s.size() - 1][0] + 1, dp[s.size() - 1][1]) << endl; return 0; }