import sys

def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  #空白なし

N = [0] + LI2()
n = len(N)

dp1 = [0]*(n)
dp2 = [0]*(n)

# Nを N_1N_2…N_(n-1) と表す
# またf(a)をa円払うときの自分と店員が使う紙幣の枚数の和の最小値とする
# このとき、f(N_1…N_k) = min(f(N_1…N_(k-1))+N_k,f(N_1…(N_(k-1)+1))+10-N_k) が成立する
# ここで、dp1[i] = f(N_1…N_i),dp2[i] = f(N_1…(N_i+1)) と定めdpする

for i in range(n):
    if i == 0:
        dp2[i] = 1
    else:
        dp1[i] = min(dp1[i-1]+N[i],dp2[i-1]+10-N[i])
        dp2[i] = min(dp1[i-1]+N[i]+1,dp2[i-1]+9-N[i])

print(dp1[n-1]) import sys sys def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  #空白なし LI2 N = [0] + LI2() N [0] + LI2() [0] 0 LI2() LI2 n = len(N) n len(N) len N dp1 = [0]*(n) dp1 [0]*(n) [0] 0 n dp2 = [0]*(n) dp2 [0]*(n) [0] 0 n for i in range(n):
    if i == 0:
        dp2[i] = 1
    else:
        dp1[i] = min(dp1[i-1]+N[i],dp2[i-1]+10-N[i])
        dp2[i] = min(dp1[i-1]+N[i]+1,dp2[i-1]+9-N[i]) i range(n) range n if i == 0:
        dp2[i] = 1
    else:
        dp1[i] = min(dp1[i-1]+N[i],dp2[i-1]+10-N[i])
        dp2[i] = min(dp1[i-1]+N[i]+1,dp2[i-1]+9-N[i]) i == 0 i 0 dp2[i] = 1 dp2[i] dp2 i 1 dp1[i] = min(dp1[i-1]+N[i],dp2[i-1]+10-N[i]) dp1[i] dp1 i min(dp1[i-1]+N[i],dp2[i-1]+10-N[i]) min dp1[i-1]+N[i] dp1[i-1] dp1 i-1 i 1 N[i] N i dp2[i-1]+10-N[i] dp2[i-1]+10 dp2[i-1] dp2 i-1 i 1 10 N[i] N i dp2[i] = min(dp1[i-1]+N[i]+1,dp2[i-1]+9-N[i]) dp2[i] dp2 i min(dp1[i-1]+N[i]+1,dp2[i-1]+9-N[i]) min dp1[i-1]+N[i]+1 dp1[i-1]+N[i] dp1[i-1] dp1 i-1 i 1 N[i] N i 1 dp2[i-1]+9-N[i] dp2[i-1]+9 dp2[i-1] dp2 i-1 i 1 9 N[i] N i print(dp1[n-1]) print dp1[n-1] dp1 n-1 n 1 dp2 = [0]*(n) [0]*(n) dp2 N = [0] + LI2() [0] + LI2() N def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  #空白なし def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  #空白なし LI2 n = len(N) len(N) n dp1 = [0]*(n) [0]*(n) dp1
import sys

def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  #空白なし

N = [0] + LI2()
n = len(N)

dp1 = [0]*(n)
dp2 = [0]*(n)

# Nを N_1N_2…N_(n-1) と表す
# またf(a)をa円払うときの自分と店員が使う紙幣の枚数の和の最小値とする
# このとき、f(N_1…N_k) = min(f(N_1…N_(k-1))+N_k,f(N_1…(N_(k-1)+1))+10-N_k) が成立する
# ここで、dp1[i] = f(N_1…N_i),dp2[i] = f(N_1…(N_i+1)) と定めdpする

for i in range(n):
    if i == 0:
        dp2[i] = 1
    else:
        dp1[i] = min(dp1[i-1]+N[i],dp2[i-1]+10-N[i])
        dp2[i] = min(dp1[i-1]+N[i]+1,dp2[i-1]+9-N[i])

print(dp1[n-1])