# e 数位dp 还可以从右到左
# 参考 https://maspypy.com/atcoder-%e5%8f%82%e5%8a%a0%e6%84%9f%e6%83%b3-2019-02-16abc-155
# https://atcoder.jp/contests/abc155/submissions/10155415
# in your code
import numpy as np
DBG=0
def p(*args, **kargs):
    if DBG:print(*args, **kargs)
read = input 
rn = lambda :list(map(int, read().split()))

def sol(s):
    dp = 0, 1 # n, n + 1
    for x in map(int, s):
        c, d = dp
    #                     为什么这里x = 0的时候不受影响 c+0 << d+10，因为abs(c-d) < 1
        
        #    前一位没借位  前一位借位
        a = min(c + x,    d + 10 - x)# for ...n
        #    这里x=9为什么不受影响, c+10 >> d，因为abs(c-d) < 1
        b = min(c + x + 1,d + 10 - 1 - x )# for ...(n+1)
        
        # 因为a,b相差小于1。f(n), f(n+1)相差总是小于1，多给1元即可
        dp = a,b
        p(dp)
    return dp[0]
if DBG:
    pass
#     sol("100")
#     assert 8 == sol("36")
#     assert sol("91") == 3
#     assert 243 == sol("314159265358979323846264338327950288419716939937551058209749445923078164062862089986280348253421170")
print(sol(read())) import numpy as np numpy DBG=0 DBG 0 def p(*args, **kargs):
    if DBG:print(*args, **kargs) p if DBG:print(*args, **ka DBG *args **kargs read = input read input rn = lambda :list(map(int, read().split())) rn lambda :list(map(int, read().split())) list(map(int, read().split())) list map(int, read().split()) map int read().split() ().split () read split def sol(s):
    dp = 0, 1 # n, n + 1
    for x in map(int, s):
        c, d = dp
    #                     为什么这里x = 0的时候不受影响 c+0 << d+10，因为abs(c-d) < 1
        
        #    前一位没借位  前一位借位
        a = min(c + x,    d + 10 - x)# for ...n
        #    这里x=9为什么不受影响, c+10 >> d，因为abs(c-d) < 1
        b = min(c + x + 1,d + 10 - 1 - x )# for ...(n+1)
        
        # 因为a,b相差小于1。f(n), f(n+1)相差总是小于1，多给1元即可
        dp = a,b
        p(dp)
    return dp[0] sol dp = 0, 1 dp dp = 0, 1 0 1 for x in map(int, s):
        c, d = dp
    #                     为什么这里x = 0的时候不受影响 c+0 << d+10，因为abs(c-d) < 1
        
        #    前一位没借位  前一位借位
        a = min(c + x,    d + 10 - x)# for ...n
        #    这里x=9为什么不受影响, c+10 >> d，因为abs(c-d) < 1
        b = min(c + x + 1,d + 10 - 1 - x )# for ...(n+1)
        
        # 因为a,b相差小于1。f(n), f(n+1)相差总是小于1，多给1元即可
        dp = a,b
        p(dp)
     x map(int, s) map int s c, d = dp c dp d a = min(c + x,    d + 10 - x) a min(c + x,    d + 10 - x) min c + x c x d + 10 - x d + 10 d 10 x b = min(c + x + 1,d + 10 - 1 - x ) b min(c + x + 1,d + 10 - 1 - x ) min c + x + 1 c + x c x 1 d + 10 - 1 - x d + 10 - 1 d + 10 d 10 1 x dp = a,b dp dp = a,b a b p(dp) p dp return dp[0] dp[0] dp 0 s s if DBG:
    pass
#     sol("100")
#     assert 8 == sol("36")
#     assert sol("91") == 3
#     assert 243 == sol("314159265358979323846264338327950288419716939937551058209749445923078164062862089986280348253421170") DBG print(sol(read())) print sol(read()) sol read() read def p(*args, **kargs):
    if DBG:print(*args, **kargs) def p(*args, **kargs):
    if DBG:print(*args, **kargs) p DBG=0 0 DBG rn = lambda :list(map(int, read().split())) lambda :list(map(int, read().split())) rn def sol(s):
    dp = 0, 1 # n, n + 1
    for x in map(int, s):
        c, d = dp
    #                     为什么这里x = 0的时候不受影响 c+0 << d+10，因为abs(c-d) < 1
        
        #    前一位没借位  前一位借位
        a = min(c + x,    d + 10 - x)# for ...n
        #    这里x=9为什么不受影响, c+10 >> d，因为abs(c-d) < 1
        b = min(c + x + 1,d + 10 - 1 - x )# for ...(n+1)
        
        # 因为a,b相差小于1。f(n), f(n+1)相差总是小于1，多给1元即可
        dp = a,b
        p(dp)
    return dp[0] def sol(s):
    dp = 0, 1 # n, n + 1
    for x in map(int, s):
        c, d = dp
    #                     为什么这里x = 0的时候不受影响 c+0 << d+10，因为abs(c-d) < 1
        
        #    前一位没借位  前一位借位
        a = min(c + x,    d + 10 - x)# for ...n
        #    这里x=9为什么不受影响, c+10 >> d，因为abs(c-d) < 1
        b = min(c + x + 1,d + 10 - 1 - x )# for ...(n+1)
        
        # 因为a,b相差小于1。f(n), f(n+1)相差总是小于1，多给1元即可
        dp = a,b
        p(dp)
    return dp[0] sol read = input input read
# e 数位dp 还可以从右到左
# 参考 https://maspypy.com/atcoder-%e5%8f%82%e5%8a%a0%e6%84%9f%e6%83%b3-2019-02-16abc-155
# https://atcoder.jp/contests/abc155/submissions/10155415
# in your code
import numpy as np
DBG=0
def p(*args, **kargs):
    if DBG:print(*args, **kargs)
read = input 
rn = lambda :list(map(int, read().split()))

def sol(s):
    dp = 0, 1 # n, n + 1
    for x in map(int, s):
        c, d = dp
    #                     为什么这里x = 0的时候不受影响 c+0 << d+10，因为abs(c-d) < 1
        
        #    前一位没借位  前一位借位
        a = min(c + x,    d + 10 - x)# for ...n
        #    这里x=9为什么不受影响, c+10 >> d，因为abs(c-d) < 1
        b = min(c + x + 1,d + 10 - 1 - x )# for ...(n+1)
        
        # 因为a,b相差小于1。f(n), f(n+1)相差总是小于1，多给1元即可
        dp = a,b
        p(dp)
    return dp[0]
if DBG:
    pass
#     sol("100")
#     assert 8 == sol("36")
#     assert sol("91") == 3
#     assert 243 == sol("314159265358979323846264338327950288419716939937551058209749445923078164062862089986280348253421170")
print(sol(read()))