import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()

def main():
    N = S()[::-1] + '0'
    dp = [[0 for _ in range(2)] for _ in range(len(N)+1)]
    dp[0][1] = inf
    for d_i in range(len(N)):
        digit = int(N[d_i])
        i = d_i + 1
        dp[i][0] = min(
            dp[i-1][0] + digit,
            dp[i-1][1] + digit + 1,
        )
        dp[i][1] = min(
            dp[i-1][0] + (10 - digit),
            dp[i-1][1] + (10 - (digit + 1))
        )
    print(dp[len(N)][0])

main()
 import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools math string itertools fractions heapq collections re array bisect sys random time copy functools from collections import deque sys.setrecursionlimit(10**7) sys.setrecursionlimit sys setrecursionlimit 10**7 10 7 inf = 10**20 inf 10**20 10 20 mod = 10**9 + 7 mod 10**9 + 7 10**9 10 9 7 DR = [1, -1, 0, 0] DR [1, -1, 0, 0] 1 -1 0 0 DC = [0, 0, 1, -1] DC [0, 0, 1, -1] 0 0 1 -1 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 int(x)-1 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)-1 int(x) int x 1 def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] LI_ float(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() float(x) float x def LF(): return [float(x) for x in sys.stdin.readline().split()] LF def LS(): return sys.stdin.readline().split() LS def I(): return int(sys.stdin.readline()) I def F(): return float(sys.stdin.readline()) F def S(): return input() S def main():
    N = S()[::-1] + '0'
    dp = [[0 for _ in range(2)] for _ in range(len(N)+1)]
    dp[0][1] = inf
    for d_i in range(len(N)):
        digit = int(N[d_i])
        i = d_i + 1
        dp[i][0] = min(
            dp[i-1][0] + digit,
            dp[i-1][1] + digit + 1,
        )
        dp[i][1] = min(
            dp[i-1][0] + (10 - digit),
            dp[i-1][1] + (10 - (digit + 1))
        )
    print(dp[len(N)][0]) main N = S()[::-1] + '0' N S()[::-1] + '0' S()[::-1] () S ::-1 -1 '0' [0 for _ in range(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) [0 for _ in range(2)] dp = [[0 for _ in range(2)] for _ in range(len(N)+1)] dp [[0 for _ in range(2)] for _ in range(len(N)+1)] dp[0][1] = inf dp[0][1] [0] dp 0 1 inf for d_i in range(len(N)):
        digit = int(N[d_i])
        i = d_i + 1
        dp[i][0] = min(
            dp[i-1][0] + digit,
            dp[i-1][1] + digit + 1,
        )
        dp[i][1] = min(
            dp[i-1][0] + (10 - digit),
            dp[i-1][1] + (10 - (digit + 1))
        )
     d_i range(len(N)) range len(N) len N digit = int(N[d_i]) digit int(N[d_i]) int N[d_i] N d_i i = d_i + 1 i d_i + 1 d_i 1 dp[i][0] = min(
            dp[i-1][0] + digit,
            dp[i-1][1] + digit + 1,
        ) dp[i][0] [i] dp i 0 min(
            dp[i-1][0] + digit,
            dp[i-1][1] + digit + 1,
        ) min dp[i-1][0] + digit dp[i-1][0] [i-1] dp i-1 i 1 0 digit dp[i-1][1] + digit + 1 dp[i-1][1] + digit dp[i-1][1] [i-1] dp i-1 i 1 1 digit 1 dp[i][1] = min(
            dp[i-1][0] + (10 - digit),
            dp[i-1][1] + (10 - (digit + 1))
        ) dp[i][1] [i] dp i 1 min(
            dp[i-1][0] + (10 - digit),
            dp[i-1][1] + (10 - (digit + 1))
        ) min dp[i-1][0] + (10 - digit) dp[i-1][0] [i-1] dp i-1 i 1 0 10 - digit 10 digit dp[i-1][1] + (10 - (digit + 1)) dp[i-1][1] [i-1] dp i-1 i 1 1 10 - (digit + 1) 10 digit + 1 digit 1 print(dp[len(N)][0]) print dp[len(N)][0] [len(N)] dp len(N) len N 0 main() main def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] LI_ def LF(): return [float(x) for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] LF def main():
    N = S()[::-1] + '0'
    dp = [[0 for _ in range(2)] for _ in range(len(N)+1)]
    dp[0][1] = inf
    for d_i in range(len(N)):
        digit = int(N[d_i])
        i = d_i + 1
        dp[i][0] = min(
            dp[i-1][0] + digit,
            dp[i-1][1] + digit + 1,
        )
        dp[i][1] = min(
            dp[i-1][0] + (10 - digit),
            dp[i-1][1] + (10 - (digit + 1))
        )
    print(dp[len(N)][0]) def main():
    N = S()[::-1] + '0'
    dp = [[0 for _ in range(2)] for _ in range(len(N)+1)]
    dp[0][1] = inf
    for d_i in range(len(N)):
        digit = int(N[d_i])
        i = d_i + 1
        dp[i][0] = min(
            dp[i-1][0] + digit,
            dp[i-1][1] + digit + 1,
        )
        dp[i][1] = min(
            dp[i-1][0] + (10 - digit),
            dp[i-1][1] + (10 - (digit + 1))
        )
    print(dp[len(N)][0]) main DC = [0, 0, 1, -1] [0, 0, 1, -1] DC def S(): return input() def S(): return input() S mod = 10**9 + 7 10**9 + 7 mod def LS(): return sys.stdin.readline().split() def LS(): return sys.stdin.readline().split() LS 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 def F(): return float(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) F DR = [1, -1, 0, 0] [1, -1, 0, 0] DR def I(): return int(sys.stdin.readline()) def I(): return int(sys.stdin.readline()) I inf = 10**20 10**20 inf
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()

def main():
    N = S()[::-1] + '0'
    dp = [[0 for _ in range(2)] for _ in range(len(N)+1)]
    dp[0][1] = inf
    for d_i in range(len(N)):
        digit = int(N[d_i])
        i = d_i + 1
        dp[i][0] = min(
            dp[i-1][0] + digit,
            dp[i-1][1] + digit + 1,
        )
        dp[i][1] = min(
            dp[i-1][0] + (10 - digit),
            dp[i-1][1] + (10 - (digit + 1))
        )
    print(dp[len(N)][0])

main()

