'''
自宅用PCでの解答
'''
import math
#import numpy as np
import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7
# mod = 998244353
dir = [(-1,0),(0,-1),(1,0),(0,1)]
alp = "abcdefghijklmnopqrstuvwxyz"
INF = 1<<32-1
# INF = 10**18

def main():
    ans = 0
    n = input()
    dp = [0,1] #0,1多く入れる
    for i in n:
        ni = int(i)
        pdp = dp*1
        dp[0] = min(pdp[0]+ni,pdp[1]+(10-ni))
        dp[1] = min(pdp[0]+ni+1,pdp[1]+(10-ni-1))
    print(dp[0])


    return None

if __name__ == '__main__':
    main() '''
自宅用PCでの解答
''' import math math import itertools itertools import queue queue import bisect bisect from collections import deque,defaultdict import heapq as hpq heapq from sys import stdin,setrecursionlimit ipt = stdin.readline ipt stdin.readline stdin readline setrecursionlimit(10**7) setrecursionlimit 10**7 10 7 mod = 10**9+7 mod 10**9+7 10**9 10 9 7 dir = [(-1,0),(0,-1),(1,0),(0,1)] dir [(-1,0),(0,-1),(1,0),(0,1)] (-1,0) -1 0 (0,-1) 0 -1 (1,0) 1 0 (0,1) 0 1 alp = "abcdefghijklmnopqrstuvwxyz" alp "abcdefghijklmnopqrstuvwxyz" INF = 1<<32-1 INF 1<<32-1 1 32-1 32 1 def main():
    ans = 0
    n = input()
    dp = [0,1] #0,1多く入れる
    for i in n:
        ni = int(i)
        pdp = dp*1
        dp[0] = min(pdp[0]+ni,pdp[1]+(10-ni))
        dp[1] = min(pdp[0]+ni+1,pdp[1]+(10-ni-1))
    print(dp[0])


    return None main ans = 0 ans 0 n = input() n input() input dp = [0,1] dp [0,1] 0 1 for i in n:
        ni = int(i)
        pdp = dp*1
        dp[0] = min(pdp[0]+ni,pdp[1]+(10-ni))
        dp[1] = min(pdp[0]+ni+1,pdp[1]+(10-ni-1))
     i n ni = int(i) ni int(i) int i pdp = dp*1 pdp dp*1 dp 1 dp[0] = min(pdp[0]+ni,pdp[1]+(10-ni)) dp[0] dp 0 min(pdp[0]+ni,pdp[1]+(10-ni)) min pdp[0]+ni pdp[0] pdp 0 ni pdp[1]+(10-ni) pdp[1] pdp 1 10-ni 10 ni dp[1] = min(pdp[0]+ni+1,pdp[1]+(10-ni-1)) dp[1] dp 1 min(pdp[0]+ni+1,pdp[1]+(10-ni-1)) min pdp[0]+ni+1 pdp[0]+ni pdp[0] pdp 0 ni 1 pdp[1]+(10-ni-1) pdp[1] pdp 1 10-ni-1 10-ni 10 ni 1 print(dp[0]) print dp[0] dp 0 return None None if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main mod = 10**9+7 10**9+7 mod dir = [(-1,0),(0,-1),(1,0),(0,1)] [(-1,0),(0,-1),(1,0),(0,1)] dir alp = "abcdefghijklmnopqrstuvwxyz" "abcdefghijklmnopqrstuvwxyz" alp INF = 1<<32-1 1<<32-1 INF ipt = stdin.readline stdin.readline ipt def main():
    ans = 0
    n = input()
    dp = [0,1] #0,1多く入れる
    for i in n:
        ni = int(i)
        pdp = dp*1
        dp[0] = min(pdp[0]+ni,pdp[1]+(10-ni))
        dp[1] = min(pdp[0]+ni+1,pdp[1]+(10-ni-1))
    print(dp[0])


    return None def main():
    ans = 0
    n = input()
    dp = [0,1] #0,1多く入れる
    for i in n:
        ni = int(i)
        pdp = dp*1
        dp[0] = min(pdp[0]+ni,pdp[1]+(10-ni))
        dp[1] = min(pdp[0]+ni+1,pdp[1]+(10-ni-1))
    print(dp[0])


    return None main
'''
自宅用PCでの解答
'''
import math
#import numpy as np
import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7
# mod = 998244353
dir = [(-1,0),(0,-1),(1,0),(0,1)]
alp = "abcdefghijklmnopqrstuvwxyz"
INF = 1<<32-1
# INF = 10**18

def main():
    ans = 0
    n = input()
    dp = [0,1] #0,1多く入れる
    for i in n:
        ni = int(i)
        pdp = dp*1
        dp[0] = min(pdp[0]+ni,pdp[1]+(10-ni))
        dp[1] = min(pdp[0]+ni+1,pdp[1]+(10-ni-1))
    print(dp[0])


    return None

if __name__ == '__main__':
    main()
