import sys



#read = sys.stdin.buffer.read

#input = sys.stdin.buffer.readline

#inputs = sys.stdin.buffer.readlines



from collections import defaultdict

# mod=10**9+7

# rstrip().decode('utf-8')

# map(int,input().split())

# import numpy as np



def main():

	li=[0]+list(map(int,eval(input())))

	n=len(li)

	

	ans=0

	

	for i in range(n):

		a=li[n-1-i]

		if 6<=a:

			ans+=10-a

			li[n-2-i]+=1

		elif a==5:

			if li[n-2-i]<=4:

				ans+=a

			else:

				ans += 10 - a

				li[n - 2 - i] += 1

		else:

			ans+=a

	print(ans)

	

	

if __name__ == "__main__":

	main()

import sys

input = sys.stdin.buffer.readline



#sys.setrecursionlimit(10**9)

#from functools import lru_cache



def RD(): return input().rstrip().decode()

def II(): return int(eval(input()))

def FI(): return int(eval(input()))

def MI(): return list(map(int,input().split()))

def MF(): return list(map(float,input().split()))

def LI(): return list(map(int,input().split()))

def LF(): return list(map(float,input().split()))

def TI(): return tuple(map(int,input().split()))

# rstrip().decode()





def main():

	n=RD()[::-1]

	N=list(map(int,n))+[0]

	#print(N)



	ans=0



	for i in range(len(n)+1):

		if N[i]<=4:

			ans+=N[i]

		elif N[i]>=6:

			ans+=10-N[i]

			N[i+1]+=1

		else:

			if N[i+1]<=4:

				ans+=N[i]

			else:

				ans+=N[i]

				N[i+1]+=1



	print(ans)



















if __name__ == "__main__":

	main()

import sys

# read = sys.stdin.buffer.read
# input = sys.stdin.buffer.readline
# inputs = sys.stdin.buffer.readlines
from collections import defaultdict

# mod=10**9+7
# rstrip().decode('utf-8')
# map(int,input().split())
# import numpy as np
def main():
    li = [0] + list(map(int, eval(input())))
    n = len(li)
    ans = 0
    for i in range(n):
        a = li[n - 1 - i]
        if 6 <= a:
            ans += 10 - a
            li[n - 2 - i] += 1
        elif a == 5:
            if li[n - 2 - i] <= 4:
                ans += a
            else:
                ans += 10 - a
                li[n - 2 - i] += 1
        else:
            ans += a
    print(ans)


if __name__ == "__main__":
    main()

import sys

input = sys.stdin.buffer.readline
# sys.setrecursionlimit(10**9)
# from functools import lru_cache
def RD():
    return input().rstrip().decode()


def II():
    return int(eval(input()))


def FI():
    return int(eval(input()))


def MI():
    return list(map(int, input().split()))


def MF():
    return list(map(float, input().split()))


def LI():
    return list(map(int, input().split()))


def LF():
    return list(map(float, input().split()))


def TI():
    return tuple(map(int, input().split()))


# rstrip().decode()
def main():
    n = RD()[::-1]
    N = list(map(int, n)) + [0]
    # print(N)
    ans = 0
    for i in range(len(n) + 1):
        if N[i] <= 4:
            ans += N[i]
        elif N[i] >= 6:
            ans += 10 - N[i]
            N[i + 1] += 1
        else:
            if N[i + 1] <= 4:
                ans += N[i]
            else:
                ans += N[i]
                N[i + 1] += 1
    print(ans)


if __name__ == "__main__":
    main()

-# read = sys.stdin.buffer.read -# input = sys.stdin.buffer.readline -# inputs = sys.stdin.buffer.readlines -from collections import defaultdict +input = sys.stdin.buffer.readline +# sys.setrecursionlimit(10**9) +# from functools import lru_cache +def RD(): +    return input().rstrip().decode() -# mod=10**9+7 -# rstrip().decode('utf-8') -# map(int,input().split()) -# import numpy as np + +def II(): +    return int(eval(input())) + + +def FI(): +    return int(eval(input())) + + +def MI(): +    return list(map(int, input().split())) + + +def MF(): +    return list(map(float, input().split())) + + +def LI(): +    return list(map(int, input().split())) + + +def LF(): +    return list(map(float, input().split())) + + +def TI(): +    return tuple(map(int, input().split())) + + +# rstrip().decode() -    li = [0] + list(map(int, eval(input()))) -    n = len(li) +    n = RD()[::-1] +    N = list(map(int, n)) + [0] +    # print(N) -    for i in range(n): -        a = li[n - 1 - i] -        if 6 <= a: -            ans += 10 - a -            li[n - 2 - i] += 1 -        elif a == 5: -            if li[n - 2 - i] <= 4: -                ans += a +    for i in range(len(n) + 1): +        if N[i] <= 4: +            ans += N[i] +        elif N[i] >= 6: +            ans += 10 - N[i] +            N[i + 1] += 1 +        else: +            if N[i + 1] <= 4: +                ans += N[i] -                ans += 10 - a -                li[n - 2 - i] += 1 -        else: -            ans += a +                ans += N[i] +                N[i + 1] += 1