#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from itertools import permutations
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = list(map(int, "0"+input()))[::-1]
    l = len(n)
    dp = [n[0],10-n[0]]
    for i in range(1,l):
        ndp = [float("inf")]*2
        x = n[i]
        for j in range(2):
            x += j
            if x < 10:
                nd = dp[j]+x
                if nd < ndp[0]:
                    ndp[0] = nd
            if x > 0:
                nd = dp[j]+(10-x)
                if nd < ndp[1]:
                    ndp[1] = nd
        dp = [i for i in ndp]
    print(dp[0])
    return

#Solve
if __name__ == "__main__":
    solve() from collections import defaultdict,deque from heapq import heappush, heappop from itertools import permutations import sys sys import math math import bisect bisect int(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() int(x) int x def LI(): return [int(x) for x in sys.stdin.readline().split()] LI def I(): return int(sys.stdin.readline()) I list(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() list(x) list x def LS():return [list(x) for x in sys.stdin.readline().split()] LS def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res S res = list(sys.stdin.readline()) res list(sys.stdin.readline()) list sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline if res[-1] == "\n":
        return res[:-1]
     res[-1] == "\n" res[-1] res -1 "\n" return res[:-1] res[:-1] res :-1 -1 return res res def IR(n):
    return [I() for i in range(n)] IR I() for i in range(n) for i in range(n) i range(n) range n for i in range(n) I() I return [I() for i in range(n)] [I() for i in range(n)] n n def LIR(n):
    return [LI() for i in range(n)] LIR LI() for i in range(n) for i in range(n) i range(n) range n for i in range(n) LI() LI return [LI() for i in range(n)] [LI() for i in range(n)] n n def SR(n):
    return [S() for i in range(n)] SR S() for i in range(n) for i in range(n) i range(n) range n for i in range(n) S() S return [S() for i in range(n)] [S() for i in range(n)] n n def LSR(n):
    return [LS() for i in range(n)] LSR LS() for i in range(n) for i in range(n) i range(n) range n for i in range(n) LS() LS return [LS() for i in range(n)] [LS() for i in range(n)] n n sys.setrecursionlimit(1000000) sys.setrecursionlimit sys setrecursionlimit 1000000 mod = 1000000007 mod 1000000007 def solve():
    n = list(map(int, "0"+input()))[::-1]
    l = len(n)
    dp = [n[0],10-n[0]]
    for i in range(1,l):
        ndp = [float("inf")]*2
        x = n[i]
        for j in range(2):
            x += j
            if x < 10:
                nd = dp[j]+x
                if nd < ndp[0]:
                    ndp[0] = nd
            if x > 0:
                nd = dp[j]+(10-x)
                if nd < ndp[1]:
                    ndp[1] = nd
        dp = [i for i in ndp]
    print(dp[0])
    return

#Solve solve n = list(map(int, "0"+input()))[::-1] n list(map(int, "0"+input()))[::-1] (map(int, "0"+input())) list map(int, "0"+input()) map int "0"+input() "0" input() input ::-1 -1 l = len(n) l len(n) len n dp = [n[0],10-n[0]] dp [n[0],10-n[0]] n[0] n 0 10-n[0] 10 n[0] n 0 for i in range(1,l):
        ndp = [float("inf")]*2
        x = n[i]
        for j in range(2):
            x += j
            if x < 10:
                nd = dp[j]+x
                if nd < ndp[0]:
                    ndp[0] = nd
            if x > 0:
                nd = dp[j]+(10-x)
                if nd < ndp[1]:
                    ndp[1] = nd
        dp = [i for i in ndp]
     i range(1,l) range 1 l ndp = [float("inf")]*2 ndp [float("inf")]*2 [float("inf")] float("inf") float "inf" 2 x = n[i] x n[i] n i for j in range(2):
            x += j
            if x < 10:
                nd = dp[j]+x
                if nd < ndp[0]:
                    ndp[0] = nd
            if x > 0:
                nd = dp[j]+(10-x)
                if nd < ndp[1]:
                    ndp[1] = nd
         j range(2) range 2 x += j x j if x < 10:
                nd = dp[j]+x
                if nd < ndp[0]:
                    ndp[0] = nd
             x < 10 x 10 nd = dp[j]+x nd dp[j]+x dp[j] dp j x if nd < ndp[0]:
                    ndp[0] = nd
             nd < ndp[0] nd ndp[0] ndp 0 ndp[0] = nd ndp[0] ndp 0 nd if x > 0:
                nd = dp[j]+(10-x)
                if nd < ndp[1]:
                    ndp[1] = nd
         x > 0 x 0 nd = dp[j]+(10-x) nd dp[j]+(10-x) dp[j] dp j 10-x 10 x if nd < ndp[1]:
                    ndp[1] = nd
         nd < ndp[1] nd ndp[1] ndp 1 ndp[1] = nd ndp[1] ndp 1 nd i for i in ndp for i in ndp i ndp for i in ndp i dp = [i for i in ndp] dp [i for i in ndp] print(dp[0]) print dp[0] dp 0 return if __name__ == "__main__":
    solve() __name__ == "__main__" __name__ "__main__" solve() solve def LSR(n):
    return [LS() for i in range(n)] def LSR(n):
    return [LS() for i in range(n)] LSR def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res S def solve():
    n = list(map(int, "0"+input()))[::-1]
    l = len(n)
    dp = [n[0],10-n[0]]
    for i in range(1,l):
        ndp = [float("inf")]*2
        x = n[i]
        for j in range(2):
            x += j
            if x < 10:
                nd = dp[j]+x
                if nd < ndp[0]:
                    ndp[0] = nd
            if x > 0:
                nd = dp[j]+(10-x)
                if nd < ndp[1]:
                    ndp[1] = nd
        dp = [i for i in ndp]
    print(dp[0])
    return

#Solve def solve():
    n = list(map(int, "0"+input()))[::-1]
    l = len(n)
    dp = [n[0],10-n[0]]
    for i in range(1,l):
        ndp = [float("inf")]*2
        x = n[i]
        for j in range(2):
            x += j
            if x < 10:
                nd = dp[j]+x
                if nd < ndp[0]:
                    ndp[0] = nd
            if x > 0:
                nd = dp[j]+(10-x)
                if nd < ndp[1]:
                    ndp[1] = nd
        dp = [i for i in ndp]
    print(dp[0])
    return

#Solve solve def LS():return [list(x) for x in sys.stdin.readline().split()] def LS():return [list(x) for x in sys.stdin.readline().split()] LS def I(): return int(sys.stdin.readline()) def I(): return int(sys.stdin.readline()) I def SR(n):
    return [S() for i in range(n)] def SR(n):
    return [S() for i in range(n)] SR def LIR(n):
    return [LI() for i in range(n)] def LIR(n):
    return [LI() for i in range(n)] LIR def IR(n):
    return [I() for i in range(n)] def IR(n):
    return [I() for i in range(n)] IR def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI(): return [int(x) for x in sys.stdin.readline().split()] LI mod = 1000000007 1000000007 mod
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from itertools import permutations
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = list(map(int, "0"+input()))[::-1]
    l = len(n)
    dp = [n[0],10-n[0]]
    for i in range(1,l):
        ndp = [float("inf")]*2
        x = n[i]
        for j in range(2):
            x += j
            if x < 10:
                nd = dp[j]+x
                if nd < ndp[0]:
                    ndp[0] = nd
            if x > 0:
                nd = dp[j]+(10-x)
                if nd < ndp[1]:
                    ndp[1] = nd
        dp = [i for i in ndp]
    print(dp[0])
    return

#Solve
if __name__ == "__main__":
    solve()
