import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

# 引かれる数の左からi桁目に+10 & 引く数のi-1桁目に+1 を適宜行う

def calc(x):
    if 0 <= x <= 5:
        return x
    else:
        return 10-x+1

S = list(input())
L = len(S)

dp0 = [0]*L
dp1 = [0]*L
dp0[0] = calc(int(S[0]))
dp1[0] = calc(int(S[0])+1)

for i in range(1,L):
    n = int(S[i])
    if n == 0:
        dp0[i] = dp0[i-1]
    else:
        dp0[i] = min(dp0[i-1]+n, dp1[i-1]+10-n)
    if n == 9:
        dp1[i] = dp1[i-1]
    else:
        dp1[i] = min(dp0[i-1]+n+1, dp1[i-1]+9-n)

print(dp0[-1]) import sys sys import math math from collections import defaultdict sys.setrecursionlimit(10**7) sys.setrecursionlimit sys setrecursionlimit 10**7 10 7 def input():
    return sys.stdin.readline()[:-1] input return sys.stdin.readline()[:-1] sys.stdin.readline()[:-1] sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline :-1 -1 mod = 10**9 + 7 mod 10**9 + 7 10**9 10 9 7 def I(): return int(input()) I def LI(): return list(map(int, input().split())) LI def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

# 引かれる数の左からi桁目に+10 & 引く数のi-1桁目に+1 を適宜行う LIR if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

# 引かれる数の左からi桁目に+10 & 引く数のi-1桁目に+1 を適宜行う row <= 0 row 0 [] for _ in range(col) for _ in range(col) _ range(col) range col for _ in range(col) [] return [[] for _ in range(col)] [[] for _ in range(col)] elif col == 1:
        return [I() for _ in range(row)]
     col == 1 col 1 I() for _ in range(row) for _ in range(row) _ range(row) range row for _ in range(row) I() I return [I() for _ in range(row)] [I() for _ in range(row)] LI() for _ in range(row) for _ in range(row) _ range(row) range row for _ in range(row) LI() LI read_all = [LI() for _ in range(row)] read_all [LI() for _ in range(row)] return map(list, zip(*read_all)) map(list, zip(*read_all)) map list zip(*read_all) zip *read_all read_all row row col col def calc(x):
    if 0 <= x <= 5:
        return x
    else:
        return 10-x+1 calc if 0 <= x <= 5:
        return x
    else:
        return 10-x+1 0 <= x <= 5 0 <= x 0 x 5 return x x return 10-x+1 10-x+1 10-x 10 x 1 x x S = list(input()) S list(input()) list input() input L = len(S) L len(S) len S dp0 = [0]*L dp0 [0]*L [0] 0 L dp1 = [0]*L dp1 [0]*L [0] 0 L dp0[0] = calc(int(S[0])) dp0[0] dp0 0 calc(int(S[0])) calc int(S[0]) int S[0] S 0 dp1[0] = calc(int(S[0])+1) dp1[0] dp1 0 calc(int(S[0])+1) calc int(S[0])+1 int(S[0]) int S[0] S 0 1 for i in range(1,L):
    n = int(S[i])
    if n == 0:
        dp0[i] = dp0[i-1]
    else:
        dp0[i] = min(dp0[i-1]+n, dp1[i-1]+10-n)
    if n == 9:
        dp1[i] = dp1[i-1]
    else:
        dp1[i] = min(dp0[i-1]+n+1, dp1[i-1]+9-n) i range(1,L) range 1 L n = int(S[i]) n int(S[i]) int S[i] S i if n == 0:
        dp0[i] = dp0[i-1]
    else:
        dp0[i] = min(dp0[i-1]+n, dp1[i-1]+10-n)
     n == 0 n 0 dp0[i] = dp0[i-1] dp0[i] dp0 i dp0[i-1] dp0 i-1 i 1 dp0[i] = min(dp0[i-1]+n, dp1[i-1]+10-n) dp0[i] dp0 i min(dp0[i-1]+n, dp1[i-1]+10-n) min dp0[i-1]+n dp0[i-1] dp0 i-1 i 1 n dp1[i-1]+10-n dp1[i-1]+10 dp1[i-1] dp1 i-1 i 1 10 n if n == 9:
        dp1[i] = dp1[i-1]
    else:
        dp1[i] = min(dp0[i-1]+n+1, dp1[i-1]+9-n) n == 9 n 9 dp1[i] = dp1[i-1] dp1[i] dp1 i dp1[i-1] dp1 i-1 i 1 dp1[i] = min(dp0[i-1]+n+1, dp1[i-1]+9-n) dp1[i] dp1 i min(dp0[i-1]+n+1, dp1[i-1]+9-n) min dp0[i-1]+n+1 dp0[i-1]+n dp0[i-1] dp0 i-1 i 1 n 1 dp1[i-1]+9-n dp1[i-1]+9 dp1[i-1] dp1 i-1 i 1 9 n print(dp0[-1]) print dp0[-1] dp0 -1 def input():
    return sys.stdin.readline()[:-1] def input():
    return sys.stdin.readline()[:-1] input S = list(input()) list(input()) S def I(): return int(input()) def I(): return int(input()) I dp1 = [0]*L [0]*L dp1 def LI(): return list(map(int, input().split())) def LI(): return list(map(int, input().split())) LI def calc(x):
    if 0 <= x <= 5:
        return x
    else:
        return 10-x+1 def calc(x):
    if 0 <= x <= 5:
        return x
    else:
        return 10-x+1 calc dp0 = [0]*L [0]*L dp0 def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

# 引かれる数の左からi桁目に+10 & 引く数のi-1桁目に+1 を適宜行う def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

# 引かれる数の左からi桁目に+10 & 引く数のi-1桁目に+1 を適宜行う LIR mod = 10**9 + 7 10**9 + 7 mod L = len(S) len(S) L n = int(S[i]) int(S[i]) n
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

# 引かれる数の左からi桁目に+10 & 引く数のi-1桁目に+1 を適宜行う

def calc(x):
    if 0 <= x <= 5:
        return x
    else:
        return 10-x+1

S = list(input())
L = len(S)

dp0 = [0]*L
dp1 = [0]*L
dp0[0] = calc(int(S[0]))
dp1[0] = calc(int(S[0])+1)

for i in range(1,L):
    n = int(S[i])
    if n == 0:
        dp0[i] = dp0[i-1]
    else:
        dp0[i] = min(dp0[i-1]+n, dp1[i-1]+10-n)
    if n == 9:
        dp1[i] = dp1[i-1]
    else:
        dp1[i] = min(dp0[i-1]+n+1, dp1[i-1]+9-n)

print(dp0[-1])