import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    s = S()
    a = [int(c) for c in s]

    dp = [0, 1]
    for c in a:
        np = [dp[0] + c, dp[0] + c + 1]
        if np[0] > dp[1] + (10 - c):
            np[0] = dp[1] + (10 - c)
        if np[1] > dp[1] + (9 - c):
            np[1] = dp[1] + (9 - c)
        dp = np

    return dp[0]

print(main())


 import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools math string itertools fractions heapq collections re array bisect sys copy functools import time,random time random sys.setrecursionlimit(10**7) sys.setrecursionlimit sys setrecursionlimit 10**7 10 7 inf = 10**20 inf 10**20 10 20 eps = 1.0 / 10**10 eps 1.0 / 10**10 1.0 10**10 10 10 mod = 10**9+7 mod 10**9+7 10**9 10 9 7 mod2 = 998244353 mod2 998244353 dd = [(-1,0),(0,1),(1,0),(0,-1)] dd [(-1,0),(0,1),(1,0),(0,-1)] (-1,0) -1 0 (0,1) 0 1 (1,0) 1 0 (0,-1) 0 -1 ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] ddn [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] (-1,0) -1 0 (-1,1) -1 1 (0,1) 0 1 (1,1) 1 1 (1,0) 1 0 (1,-1) 1 -1 (0,-1) 0 -1 (-1,-1) -1 -1 def LI(): return list(map(int, sys.stdin.readline().split())) LI list(map(int, l.split())) for l in sys.stdin.readlines() for l in sys.stdin.readlines() l sys.stdin.readlines() sys.stdin.readlines sys.stdin sys stdin readlines for l in sys.stdin.readlines() list(map(int, l.split())) list map(int, l.split()) map int l.split() l.split l split def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()] LLI int(x)-1 for x in sys.stdin.readline().split() for x in sys.stdin.readline().split() x sys.stdin.readline().split() sys.stdin.readline().split sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline split for x in sys.stdin.readline().split() int(x)-1 int(x) int x 1 def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] LI_ float(x) for x in sys.stdin.readline().split() for x in sys.stdin.readline().split() x sys.stdin.readline().split() sys.stdin.readline().split sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline split for x in sys.stdin.readline().split() float(x) float x def LF(): return [float(x) for x in sys.stdin.readline().split()] LF def LS(): return sys.stdin.readline().split() LS def I(): return int(sys.stdin.readline()) I def F(): return float(sys.stdin.readline()) F def S(): return input() S def pf(s): return print(s, flush=True) pf s s def pe(s): return print(str(s), file=sys.stderr) pe s s def JA(a, sep): return sep.join(map(str, a)) JA a a sep sep def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a) JAA a a s s t t def main():
    s = S()
    a = [int(c) for c in s]

    dp = [0, 1]
    for c in a:
        np = [dp[0] + c, dp[0] + c + 1]
        if np[0] > dp[1] + (10 - c):
            np[0] = dp[1] + (10 - c)
        if np[1] > dp[1] + (9 - c):
            np[1] = dp[1] + (9 - c)
        dp = np

    return dp[0] main s = S() s S() S int(c) for c in s for c in s c s for c in s int(c) int c a = [int(c) for c in s] a [int(c) for c in s] dp = [0, 1] dp [0, 1] 0 1 for c in a:
        np = [dp[0] + c, dp[0] + c + 1]
        if np[0] > dp[1] + (10 - c):
            np[0] = dp[1] + (10 - c)
        if np[1] > dp[1] + (9 - c):
            np[1] = dp[1] + (9 - c)
        dp = np

     c a np = [dp[0] + c, dp[0] + c + 1] np [dp[0] + c, dp[0] + c + 1] dp[0] + c dp[0] dp 0 c dp[0] + c + 1 dp[0] + c dp[0] dp 0 c 1 if np[0] > dp[1] + (10 - c):
            np[0] = dp[1] + (10 - c)
         np[0] > dp[1] + (10 - c) np[0] np 0 dp[1] + (10 - c) dp[1] dp 1 10 - c 10 c np[0] = dp[1] + (10 - c) np[0] np 0 dp[1] + (10 - c) dp[1] dp 1 10 - c 10 c if np[1] > dp[1] + (9 - c):
            np[1] = dp[1] + (9 - c)
         np[1] > dp[1] + (9 - c) np[1] np 1 dp[1] + (9 - c) dp[1] dp 1 9 - c 9 c np[1] = dp[1] + (9 - c) np[1] np 1 dp[1] + (9 - c) dp[1] dp 1 9 - c 9 c dp = np dp np return dp[0] dp[0] dp 0 print(main()) print main() main eps = 1.0 / 10**10 1.0 / 10**10 eps def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a) def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a) JAA inf = 10**20 10**20 inf def JA(a, sep): return sep.join(map(str, a)) def JA(a, sep): return sep.join(map(str, a)) JA ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] ddn def LI(): return list(map(int, sys.stdin.readline().split())) def LI(): return list(map(int, sys.stdin.readline().split())) LI def S(): return input() def S(): return input() S def LF(): return [float(x) for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] LF def I(): return int(sys.stdin.readline()) def I(): return int(sys.stdin.readline()) I def main():
    s = S()
    a = [int(c) for c in s]

    dp = [0, 1]
    for c in a:
        np = [dp[0] + c, dp[0] + c + 1]
        if np[0] > dp[1] + (10 - c):
            np[0] = dp[1] + (10 - c)
        if np[1] > dp[1] + (9 - c):
            np[1] = dp[1] + (9 - c)
        dp = np

    return dp[0] def main():
    s = S()
    a = [int(c) for c in s]

    dp = [0, 1]
    for c in a:
        np = [dp[0] + c, dp[0] + c + 1]
        if np[0] > dp[1] + (10 - c):
            np[0] = dp[1] + (10 - c)
        if np[1] > dp[1] + (9 - c):
            np[1] = dp[1] + (9 - c)
        dp = np

    return dp[0] main def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] LI_ def F(): return float(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) F mod = 10**9+7 10**9+7 mod def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()] def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()] LLI def pf(s): return print(s, flush=True) def pf(s): return print(s, flush=True) pf def LS(): return sys.stdin.readline().split() def LS(): return sys.stdin.readline().split() LS mod2 = 998244353 998244353 mod2 def pe(s): return print(str(s), file=sys.stderr) def pe(s): return print(str(s), file=sys.stderr) pe dd = [(-1,0),(0,1),(1,0),(0,-1)] [(-1,0),(0,1),(1,0),(0,-1)] dd
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    s = S()
    a = [int(c) for c in s]

    dp = [0, 1]
    for c in a:
        np = [dp[0] + c, dp[0] + c + 1]
        if np[0] > dp[1] + (10 - c):
            np[0] = dp[1] + (10 - c)
        if np[1] > dp[1] + (9 - c):
            np[1] = dp[1] + (9 - c)
        dp = np

    return dp[0]

print(main())



