import sys
import math
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
from collections import deque
from bisect import bisect_left
from itertools import product
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
#文字列を一文字ずつ数字に変換、'5678'を[5,6,7,8]とできる
def LSI(): return list(map(int, list(sys.stdin.readline().rstrip())))
def LSI2(N): return [list(map(int, list(sys.stdin.readline().rstrip()))) for i in range(N)]
#文字列として取得
def ST(): return sys.stdin.readline().rstrip()
def LST(): return sys.stdin.readline().rstrip().split()
def LST2(N): return [sys.stdin.readline().rstrip().split() for i in range(N)]
def FILL(i,h): return [i for j in range(h)]
def FILL2(i,h,w): return [FILL(i,w) for j in range(h)]
def FILL3(i,h,w,d): return [FILL2(i,w,d) for j in range(h)]
def FILL4(i,h,w,d,d2): return [FILL3(i,w,d,d2) for j in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)
#'0.01'や'1E1'などで指定、整数に戻すならintをかます
MOD = 1000000007
INF = float("inf")
sys.setrecursionlimit(10**6+10)

N = LSI()
dp = [[INF]*2 for _ in range(len(N)+1)] #dp[i][0]が下からi桁めをぴったり払う最低枚数
dp[0][0] = 0
for i in range(len(N)):
    d = N[-(i+1)]
    dp[i+1][0] = min(dp[i][0] + d, dp[i][1] + d + 1)
    dp[i+1][1] = min(dp[i][0] + (10-d), dp[i][1] + (10-d) - 1)
print(min(dp[-1][0], dp[-1][1]+1)) import sys sys import math math from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN from collections import deque from bisect import bisect_left from itertools import product def I(): return int(sys.stdin.readline()) I def MI(): return map(int, sys.stdin.readline().split()) MI def LI(): return list(map(int, sys.stdin.readline().split())) LI list(map(int, sys.stdin.readline().split())) for i in range(N) for i in range(N) i range(N) range N for i in range(N) list(map(int, sys.stdin.readline().split())) list map(int, sys.stdin.readline().split()) map int sys.stdin.readline().split() sys.stdin.readline().split sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline split def LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
#文字列を一文字ずつ数字に変換、'5678'を[5,6,7,8]とできる LI2 N N def LSI(): return list(map(int, list(sys.stdin.readline().rstrip()))) LSI list(map(int, list(sys.stdin.readline().rstrip()))) for i in range(N) for i in range(N) i range(N) range N for i in range(N) list(map(int, list(sys.stdin.readline().rstrip()))) list map(int, list(sys.stdin.readline().rstrip())) map int list(sys.stdin.readline().rstrip()) list sys.stdin.readline().rstrip() sys.stdin.readline().rstrip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline rstrip def LSI2(N): return [list(map(int, list(sys.stdin.readline().rstrip()))) for i in range(N)]
#文字列として取得 LSI2 N N def ST(): return sys.stdin.readline().rstrip() ST def LST(): return sys.stdin.readline().rstrip().split() LST sys.stdin.readline().rstrip().split() for i in range(N) for i in range(N) i range(N) range N for i in range(N) sys.stdin.readline().rstrip().split() sys.stdin.readline().rstrip().split sys.stdin.readline().rstrip() sys.stdin.readline().rstrip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline rstrip split def LST2(N): return [sys.stdin.readline().rstrip().split() for i in range(N)] LST2 N N i for j in range(h) for j in range(h) j range(h) range h for j in range(h) i def FILL(i,h): return [i for j in range(h)] FILL i i h h FILL(i,w) for j in range(h) for j in range(h) j range(h) range h for j in range(h) FILL(i,w) FILL i w def FILL2(i,h,w): return [FILL(i,w) for j in range(h)] FILL2 i i h h w w FILL2(i,w,d) for j in range(h) for j in range(h) j range(h) range h for j in range(h) FILL2(i,w,d) FILL2 i w d def FILL3(i,h,w,d): return [FILL2(i,w,d) for j in range(h)] FILL3 i i h h w w d d FILL3(i,w,d,d2) for j in range(h) for j in range(h) j range(h) range h for j in range(h) FILL3(i,w,d,d2) FILL3 i w d d2 def FILL4(i,h,w,d,d2): return [FILL3(i,w,d,d2) for j in range(h)] FILL4 i i h h w w d d d2 d2 def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)
#'0.01'や'1E1'などで指定、整数に戻すならintをかます sisha num num digit digit MOD = 1000000007 MOD 1000000007 INF = float("inf") INF float("inf") float "inf" sys.setrecursionlimit(10**6+10) sys.setrecursionlimit sys setrecursionlimit 10**6+10 10**6 10 6 10 N = LSI() N LSI() LSI [INF]*2 for _ in range(len(N)+1) for _ in range(len(N)+1) _ range(len(N)+1) range len(N)+1 len(N) len N 1 for _ in range(len(N)+1) [INF]*2 [INF] INF 2 dp = [[INF]*2 for _ in range(len(N)+1)] dp [[INF]*2 for _ in range(len(N)+1)] dp[0][0] = 0 dp[0][0] [0] dp 0 0 0 for i in range(len(N)):
    d = N[-(i+1)]
    dp[i+1][0] = min(dp[i][0] + d, dp[i][1] + d + 1)
    dp[i+1][1] = min(dp[i][0] + (10-d), dp[i][1] + (10-d) - 1) i range(len(N)) range len(N) len N d = N[-(i+1)] d N[-(i+1)] N -(i+1) i+1 i 1 dp[i+1][0] = min(dp[i][0] + d, dp[i][1] + d + 1) dp[i+1][0] [i+1] dp i+1 i 1 0 min(dp[i][0] + d, dp[i][1] + d + 1) min dp[i][0] + d dp[i][0] [i] dp i 0 d dp[i][1] + d + 1 dp[i][1] + d dp[i][1] [i] dp i 1 d 1 dp[i+1][1] = min(dp[i][0] + (10-d), dp[i][1] + (10-d) - 1) dp[i+1][1] [i+1] dp i+1 i 1 1 min(dp[i][0] + (10-d), dp[i][1] + (10-d) - 1) min dp[i][0] + (10-d) dp[i][0] [i] dp i 0 10-d 10 d dp[i][1] + (10-d) - 1 dp[i][1] + (10-d) dp[i][1] [i] dp i 1 10-d 10 d 1 print(min(dp[-1][0], dp[-1][1]+1)) print min(dp[-1][0], dp[-1][1]+1) min dp[-1][0] [-1] dp -1 0 dp[-1][1]+1 dp[-1][1] [-1] dp -1 1 1 def I(): return int(sys.stdin.readline()) def I(): return int(sys.stdin.readline()) I def FILL2(i,h,w): return [FILL(i,w) for j in range(h)] def FILL2(i,h,w): return [FILL(i,w) for j in range(h)] FILL2 MOD = 1000000007 1000000007 MOD def LSI(): return list(map(int, list(sys.stdin.readline().rstrip()))) def LSI(): return list(map(int, list(sys.stdin.readline().rstrip()))) LSI def LST2(N): return [sys.stdin.readline().rstrip().split() for i in range(N)] def LST2(N): return [sys.stdin.readline().rstrip().split() for i in range(N)] LST2 INF = float("inf") float("inf") INF def MI(): return map(int, sys.stdin.readline().split()) def MI(): return map(int, sys.stdin.readline().split()) MI def LSI2(N): return [list(map(int, list(sys.stdin.readline().rstrip()))) for i in range(N)]
#文字列として取得 def LSI2(N): return [list(map(int, list(sys.stdin.readline().rstrip()))) for i in range(N)]
#文字列として取得 LSI2 def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)
#'0.01'や'1E1'などで指定、整数に戻すならintをかます def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)
#'0.01'や'1E1'などで指定、整数に戻すならintをかます sisha dp = [[INF]*2 for _ in range(len(N)+1)] [[INF]*2 for _ in range(len(N)+1)] dp def LST(): return sys.stdin.readline().rstrip().split() def LST(): return sys.stdin.readline().rstrip().split() LST def LI(): return list(map(int, sys.stdin.readline().split())) def LI(): return list(map(int, sys.stdin.readline().split())) LI def ST(): return sys.stdin.readline().rstrip() def ST(): return sys.stdin.readline().rstrip() ST def LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
#文字列を一文字ずつ数字に変換、'5678'を[5,6,7,8]とできる def LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
#文字列を一文字ずつ数字に変換、'5678'を[5,6,7,8]とできる LI2 N = LSI() LSI() N def FILL4(i,h,w,d,d2): return [FILL3(i,w,d,d2) for j in range(h)] def FILL4(i,h,w,d,d2): return [FILL3(i,w,d,d2) for j in range(h)] FILL4 def FILL3(i,h,w,d): return [FILL2(i,w,d) for j in range(h)] def FILL3(i,h,w,d): return [FILL2(i,w,d) for j in range(h)] FILL3 def FILL(i,h): return [i for j in range(h)] def FILL(i,h): return [i for j in range(h)] FILL d = N[-(i+1)] N[-(i+1)] d
import sys
import math
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
from collections import deque
from bisect import bisect_left
from itertools import product
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
#文字列を一文字ずつ数字に変換、'5678'を[5,6,7,8]とできる
def LSI(): return list(map(int, list(sys.stdin.readline().rstrip())))
def LSI2(N): return [list(map(int, list(sys.stdin.readline().rstrip()))) for i in range(N)]
#文字列として取得
def ST(): return sys.stdin.readline().rstrip()
def LST(): return sys.stdin.readline().rstrip().split()
def LST2(N): return [sys.stdin.readline().rstrip().split() for i in range(N)]
def FILL(i,h): return [i for j in range(h)]
def FILL2(i,h,w): return [FILL(i,w) for j in range(h)]
def FILL3(i,h,w,d): return [FILL2(i,w,d) for j in range(h)]
def FILL4(i,h,w,d,d2): return [FILL3(i,w,d,d2) for j in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)
#'0.01'や'1E1'などで指定、整数に戻すならintをかます
MOD = 1000000007
INF = float("inf")
sys.setrecursionlimit(10**6+10)

N = LSI()
dp = [[INF]*2 for _ in range(len(N)+1)] #dp[i][0]が下からi桁めをぴったり払う最低枚数
dp[0][0] = 0
for i in range(len(N)):
    d = N[-(i+1)]
    dp[i+1][0] = min(dp[i][0] + d, dp[i][1] + d + 1)
    dp[i+1][1] = min(dp[i][0] + (10-d), dp[i][1] + (10-d) - 1)
print(min(dp[-1][0], dp[-1][1]+1))
