import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = list(map(int, list(input())))
length = len(N)
# 丁度払うか釣りをもらうか
dp = [[10**18]*2 for _ in range(length+1)]
dp[0][0] = 0
for i in range(1, length+1):
    n = N[-i]
    dp[i][1] = min(dp[i-1][1]+(9-n), dp[i-1][0]+(10-n))
    dp[i][0] = min(dp[i-1][0]+n, dp[i-1][1]+n+1)
    if i==length:
        dp[i][1] = dp[i][1]+1
print(min(dp[-1])) import sys sys def input(): return sys.stdin.readline().strip() input def mapint(): return map(int, input().split()) mapint sys.setrecursionlimit(10**9) sys.setrecursionlimit sys setrecursionlimit 10**9 10 9 N = list(map(int, list(input()))) N list(map(int, list(input()))) list map(int, list(input())) map int list(input()) list input() input length = len(N) length len(N) len N [10**18]*2 for _ in range(length+1) for _ in range(length+1) _ range(length+1) range length+1 length 1 for _ in range(length+1) [10**18]*2 [10**18] 10**18 10 18 2 dp = [[10**18]*2 for _ in range(length+1)] dp [[10**18]*2 for _ in range(length+1)] dp[0][0] = 0 dp[0][0] [0] dp 0 0 0 for i in range(1, length+1):
    n = N[-i]
    dp[i][1] = min(dp[i-1][1]+(9-n), dp[i-1][0]+(10-n))
    dp[i][0] = min(dp[i-1][0]+n, dp[i-1][1]+n+1)
    if i==length:
        dp[i][1] = dp[i][1]+1 i range(1, length+1) range 1 length+1 length 1 n = N[-i] n N[-i] N -i i dp[i][1] = min(dp[i-1][1]+(9-n), dp[i-1][0]+(10-n)) dp[i][1] [i] dp i 1 min(dp[i-1][1]+(9-n), dp[i-1][0]+(10-n)) min dp[i-1][1]+(9-n) dp[i-1][1] [i-1] dp i-1 i 1 1 9-n 9 n dp[i-1][0]+(10-n) dp[i-1][0] [i-1] dp i-1 i 1 0 10-n 10 n dp[i][0] = min(dp[i-1][0]+n, dp[i-1][1]+n+1) dp[i][0] [i] dp i 0 min(dp[i-1][0]+n, dp[i-1][1]+n+1) min dp[i-1][0]+n dp[i-1][0] [i-1] dp i-1 i 1 0 n dp[i-1][1]+n+1 dp[i-1][1]+n dp[i-1][1] [i-1] dp i-1 i 1 1 n 1 if i==length:
        dp[i][1] = dp[i][1]+1 i==length i length dp[i][1] = dp[i][1]+1 dp[i][1] [i] dp i 1 dp[i][1]+1 dp[i][1] [i] dp i 1 1 print(min(dp[-1])) print min(dp[-1]) min dp[-1] dp -1 n = N[-i] N[-i] n dp = [[10**18]*2 for _ in range(length+1)] [[10**18]*2 for _ in range(length+1)] dp length = len(N) len(N) length def input(): return sys.stdin.readline().strip() def input(): return sys.stdin.readline().strip() input def mapint(): return map(int, input().split()) def mapint(): return map(int, input().split()) mapint N = list(map(int, list(input()))) list(map(int, list(input()))) N
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = list(map(int, list(input())))
length = len(N)
# 丁度払うか釣りをもらうか
dp = [[10**18]*2 for _ in range(length+1)]
dp[0][0] = 0
for i in range(1, length+1):
    n = N[-i]
    dp[i][1] = min(dp[i-1][1]+(9-n), dp[i-1][0]+(10-n))
    dp[i][0] = min(dp[i-1][0]+n, dp[i-1][1]+n+1)
    if i==length:
        dp[i][1] = dp[i][1]+1
print(min(dp[-1]))