#include using namespace std; #define rep(i,n) for(long long i = 0; i < (long long)(n); i++) using ll = long long; template bool chmin(T1 &a, T2 b) { return b < a && (a = b, true); } static const long long INF = 4e18; ll pay[] = {0, 1, 2, 3, 2, 1, 2, 3, 3, 2, 1}; int main(void) { string s; cin >> s; ll n = s.length(); if (n != 10001) { assert(1<=n&&n<=10000); } else { assert(n=10001); assert(s[0] == 1); rep(i, n-1) assert(s[i+1] == 0); } rep(i, n) assert('0'<=s[i]&&s[i]<='9'), s[i] -= '0'; ll a = pay[s[0]], b = pay[s[0]+1]; rep(i, s.length()-1) { ll a_next = INF, b_next = INF; chmin(a_next, min(a + pay[s[i+1]+0], b + pay[10-s[i+1]])); chmin(b_next, min(a + pay[s[i+1]+1], b + pay[9-s[i+1]])); swap(a_next, a), swap(b_next, b); } cout << a << endl; return 0; }