#define _USE_MATH_DEFINES #include "bits/stdc++.h" using namespace std; #define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i)) #define rep(i,j) FOR(i,0,j) #define each(x,y) for(auto &(x):(y)) #define mp make_pair #define MT make_tuple #define all(x) (x).begin(),(x).end() #define debug(x) cout<<#x<<": "<<(x)< >(b,vector(c,d)) #define vvv(a,b,c,d,e) vector > >(b,vv(a,c,d,e)) using ll = long long; using pii = pair; using vi = vector; using vll = vector; int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(20); string N; cin >> N; N = string("0") + N; int K = sz(N); const int INF = 1 << 29; auto dp = vv(int, 2, K + 1, INF); dp[0][0] = 0; rep(j, K) rep(i, 2) { int d = 10 * i - (N[j] - '0'); rep(ax, 2)rep(ay, 5) { int a = d + ax * 5 + ay; rep(bx, 2)rep(by, 5) { int b = a - bx * 5 - by; if (b==0||b==1) { int used = dp[i][j] + ax + ay + bx + by; smin(dp[b][j + 1], used); } } } } cout << dp[0][K] << endl; }