from sys import setrecursionlimit
n = "0"+input()
N = len(n)
point = 0
for i in range(1,N):
    one_push = 1+point+10-int(n[i])
    no_push = point + int(n[i])
    point = min(one_push,no_push)
print(point)