#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#solve
def solve():
    n = ["0"] + S()
    n = n[::-1]
    dp1 = [inf] * (len(n))
    dp2 = [inf] * (len(n))
    dp1[0] = int(n[0])
    dp2[0] = 10-int(n[0])
    for i in range(len(n)-1):
        x = int(n[i+1])
        if dp1[i] + x < dp1[i + 1]:
            dp1[i + 1] = dp1[i] + x
        if x != 0:
            if dp1[i] + (10 - x) < dp2[i + 1]:
                dp2[i + 1] = dp1[i] + (10 - x)
        x += 1
        if x != 10:
            if dp2[i] + x < dp1[i + 1]:
                dp1[i + 1] = dp2[i] + x
        if dp2[i] + (10 - x) < dp2[i + 1]:
            dp2[i + 1] = dp2[i] + (10 - x)

    print(min(dp1[-1],dp2[-1]))
    return


#main
if __name__ == '__main__':
    solve() from collections import defaultdict,deque from heapq import heappush, heappop from bisect import bisect_left, bisect_right import sys, random, itertools, math sys random itertools math sys.setrecursionlimit(10**5) sys.setrecursionlimit sys setrecursionlimit 10**5 10 5 input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline sqrt = math.sqrt sqrt math.sqrt math sqrt def LI(): return list(map(int, input().split())) LI def LF(): return list(map(float, input().split())) LF def LI_(): return list(map(lambda x: int(x)-1, input().split())) LI_ def II(): return int(input()) II def IF(): return float(input()) IF def LS(): return list(map(list, input().split())) LS def S(): return list(input().rstrip()) S II() for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) II() II def IR(n): return [II() for _ in range(n)] IR n n LI() for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) LI() LI def LIR(n): return [LI() for _ in range(n)] LIR n n IF() for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) IF() IF def FR(n): return [IF() for _ in range(n)] FR n n LI() for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) LI() LI def LFR(n): return [LI() for _ in range(n)] LFR n n LI_() for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) LI_() LI_ def LIR_(n): return [LI_() for _ in range(n)] LIR_ n n S() for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) S() S def SR(n): return [S() for _ in range(n)] SR n n LS() for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) LS() LS def LSR(n): return [LS() for _ in range(n)] LSR n n mod = 1000000007 mod 1000000007 inf = float('INF') inf float('INF') float 'INF' def solve():
    n = ["0"] + S()
    n = n[::-1]
    dp1 = [inf] * (len(n))
    dp2 = [inf] * (len(n))
    dp1[0] = int(n[0])
    dp2[0] = 10-int(n[0])
    for i in range(len(n)-1):
        x = int(n[i+1])
        if dp1[i] + x < dp1[i + 1]:
            dp1[i + 1] = dp1[i] + x
        if x != 0:
            if dp1[i] + (10 - x) < dp2[i + 1]:
                dp2[i + 1] = dp1[i] + (10 - x)
        x += 1
        if x != 10:
            if dp2[i] + x < dp1[i + 1]:
                dp1[i + 1] = dp2[i] + x
        if dp2[i] + (10 - x) < dp2[i + 1]:
            dp2[i + 1] = dp2[i] + (10 - x)

    print(min(dp1[-1],dp2[-1]))
    return


#main solve n = ["0"] + S() n ["0"] + S() ["0"] "0" S() S n = n[::-1] n n[::-1] n ::-1 -1 dp1 = [inf] * (len(n)) dp1 [inf] * (len(n)) [inf] inf len(n) len n dp2 = [inf] * (len(n)) dp2 [inf] * (len(n)) [inf] inf len(n) len n dp1[0] = int(n[0]) dp1[0] dp1 0 int(n[0]) int n[0] n 0 dp2[0] = 10-int(n[0]) dp2[0] dp2 0 10-int(n[0]) 10 int(n[0]) int n[0] n 0 for i in range(len(n)-1):
        x = int(n[i+1])
        if dp1[i] + x < dp1[i + 1]:
            dp1[i + 1] = dp1[i] + x
        if x != 0:
            if dp1[i] + (10 - x) < dp2[i + 1]:
                dp2[i + 1] = dp1[i] + (10 - x)
        x += 1
        if x != 10:
            if dp2[i] + x < dp1[i + 1]:
                dp1[i + 1] = dp2[i] + x
        if dp2[i] + (10 - x) < dp2[i + 1]:
            dp2[i + 1] = dp2[i] + (10 - x)

     i range(len(n)-1) range len(n)-1 len(n) len n 1 x = int(n[i+1]) x int(n[i+1]) int n[i+1] n i+1 i 1 if dp1[i] + x < dp1[i + 1]:
            dp1[i + 1] = dp1[i] + x
         dp1[i] + x < dp1[i + 1] dp1[i] + x dp1[i] dp1 i x dp1[i + 1] dp1 i + 1 i 1 dp1[i + 1] = dp1[i] + x dp1[i + 1] dp1 i + 1 i 1 dp1[i] + x dp1[i] dp1 i x if x != 0:
            if dp1[i] + (10 - x) < dp2[i + 1]:
                dp2[i + 1] = dp1[i] + (10 - x)
         x != 0 x 0 if dp1[i] + (10 - x) < dp2[i + 1]:
                dp2[i + 1] = dp1[i] + (10 - x)
         dp1[i] + (10 - x) < dp2[i + 1] dp1[i] + (10 - x) dp1[i] dp1 i 10 - x 10 x dp2[i + 1] dp2 i + 1 i 1 dp2[i + 1] = dp1[i] + (10 - x) dp2[i + 1] dp2 i + 1 i 1 dp1[i] + (10 - x) dp1[i] dp1 i 10 - x 10 x x += 1 x 1 if x != 10:
            if dp2[i] + x < dp1[i + 1]:
                dp1[i + 1] = dp2[i] + x
         x != 10 x 10 if dp2[i] + x < dp1[i + 1]:
                dp1[i + 1] = dp2[i] + x
         dp2[i] + x < dp1[i + 1] dp2[i] + x dp2[i] dp2 i x dp1[i + 1] dp1 i + 1 i 1 dp1[i + 1] = dp2[i] + x dp1[i + 1] dp1 i + 1 i 1 dp2[i] + x dp2[i] dp2 i x if dp2[i] + (10 - x) < dp2[i + 1]:
            dp2[i + 1] = dp2[i] + (10 - x)

     dp2[i] + (10 - x) < dp2[i + 1] dp2[i] + (10 - x) dp2[i] dp2 i 10 - x 10 x dp2[i + 1] dp2 i + 1 i 1 dp2[i + 1] = dp2[i] + (10 - x) dp2[i + 1] dp2 i + 1 i 1 dp2[i] + (10 - x) dp2[i] dp2 i 10 - x 10 x print(min(dp1[-1],dp2[-1])) print min(dp1[-1],dp2[-1]) min dp1[-1] dp1 -1 dp2[-1] dp2 -1 return if __name__ == '__main__':
    solve() __name__ == '__main__' __name__ '__main__' solve() solve inf = float('INF') float('INF') inf def LIR(n): return [LI() for _ in range(n)] def LIR(n): return [LI() for _ in range(n)] LIR def SR(n): return [S() for _ in range(n)] def SR(n): return [S() for _ in range(n)] SR def LF(): return list(map(float, input().split())) def LF(): return list(map(float, input().split())) LF def LS(): return list(map(list, input().split())) def LS(): return list(map(list, input().split())) LS def LIR_(n): return [LI_() for _ in range(n)] def LIR_(n): return [LI_() for _ in range(n)] LIR_ def solve():
    n = ["0"] + S()
    n = n[::-1]
    dp1 = [inf] * (len(n))
    dp2 = [inf] * (len(n))
    dp1[0] = int(n[0])
    dp2[0] = 10-int(n[0])
    for i in range(len(n)-1):
        x = int(n[i+1])
        if dp1[i] + x < dp1[i + 1]:
            dp1[i + 1] = dp1[i] + x
        if x != 0:
            if dp1[i] + (10 - x) < dp2[i + 1]:
                dp2[i + 1] = dp1[i] + (10 - x)
        x += 1
        if x != 10:
            if dp2[i] + x < dp1[i + 1]:
                dp1[i + 1] = dp2[i] + x
        if dp2[i] + (10 - x) < dp2[i + 1]:
            dp2[i + 1] = dp2[i] + (10 - x)

    print(min(dp1[-1],dp2[-1]))
    return


#main def solve():
    n = ["0"] + S()
    n = n[::-1]
    dp1 = [inf] * (len(n))
    dp2 = [inf] * (len(n))
    dp1[0] = int(n[0])
    dp2[0] = 10-int(n[0])
    for i in range(len(n)-1):
        x = int(n[i+1])
        if dp1[i] + x < dp1[i + 1]:
            dp1[i + 1] = dp1[i] + x
        if x != 0:
            if dp1[i] + (10 - x) < dp2[i + 1]:
                dp2[i + 1] = dp1[i] + (10 - x)
        x += 1
        if x != 10:
            if dp2[i] + x < dp1[i + 1]:
                dp1[i + 1] = dp2[i] + x
        if dp2[i] + (10 - x) < dp2[i + 1]:
            dp2[i + 1] = dp2[i] + (10 - x)

    print(min(dp1[-1],dp2[-1]))
    return


#main solve def IR(n): return [II() for _ in range(n)] def IR(n): return [II() for _ in range(n)] IR def LFR(n): return [LI() for _ in range(n)] def LFR(n): return [LI() for _ in range(n)] LFR def FR(n): return [IF() for _ in range(n)] def FR(n): return [IF() for _ in range(n)] FR sqrt = math.sqrt math.sqrt sqrt def IF(): return float(input()) def IF(): return float(input()) IF input = sys.stdin.readline sys.stdin.readline input def LI_(): return list(map(lambda x: int(x)-1, input().split())) def LI_(): return list(map(lambda x: int(x)-1, input().split())) LI_ def LSR(n): return [LS() for _ in range(n)] def LSR(n): return [LS() for _ in range(n)] LSR def S(): return list(input().rstrip()) def S(): return list(input().rstrip()) S def LI(): return list(map(int, input().split())) def LI(): return list(map(int, input().split())) LI def II(): return int(input()) def II(): return int(input()) II mod = 1000000007 1000000007 mod
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#solve
def solve():
    n = ["0"] + S()
    n = n[::-1]
    dp1 = [inf] * (len(n))
    dp2 = [inf] * (len(n))
    dp1[0] = int(n[0])
    dp2[0] = 10-int(n[0])
    for i in range(len(n)-1):
        x = int(n[i+1])
        if dp1[i] + x < dp1[i + 1]:
            dp1[i + 1] = dp1[i] + x
        if x != 0:
            if dp1[i] + (10 - x) < dp2[i + 1]:
                dp2[i + 1] = dp1[i] + (10 - x)
        x += 1
        if x != 10:
            if dp2[i] + x < dp1[i + 1]:
                dp1[i + 1] = dp2[i] + x
        if dp2[i] + (10 - x) < dp2[i + 1]:
            dp2[i + 1] = dp2[i] + (10 - x)

    print(min(dp1[-1],dp2[-1]))
    return


#main
if __name__ == '__main__':
    solve()
