# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

##### 無限 #####
# inf = float('inf')

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

inf = float('inf')

def main():
	N = input()
	N = N[:-1]
	N = "0" + N
	N = N[::-1]

	dp = [[inf,inf] for i in range(len(N)+2)]
	dp[0][0] = 0
	#print(dp)

	cnt = 0
	for ii in N:
		i = int(ii)
		cnt += 1
		#print(dp)
		for j in range(10):
			if i > j:
				tmp = j + 10 - i
				dp[cnt][1] = min(dp[cnt][1], dp[cnt-1][0] + tmp + j)
			else:
				tmp = j - i
				dp[cnt][0] = min(dp[cnt][0], dp[cnt-1][0] + tmp + j)

			tmptmp = j-1
			if i > tmptmp:
				tmp = tmptmp + 10 - i
				dp[cnt][1] = min(dp[cnt][1], dp[cnt-1][1] + tmp + j)
			else:
				tmp = tmptmp - i
				dp[cnt][0] = min(dp[cnt][0], dp[cnt-1][1] + tmp + j)

	#print(dp)
	print(dp[len(N)][0])

if __name__ == "__main__":
	main() import sys sys import math math from bisect import bisect_left from bisect import bisect_right import collections collections import copy copy import heapq heapq from collections import defaultdict from heapq import heappop, heappush import itertools itertools input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline def inputInt(): return int(input()) inputInt def inputMap(): return map(int, input().split()) inputMap def inputList(): return list(map(int, input().split())) inputList inf = float('inf') inf float('inf') float 'inf' def main():
	N = input()
	N = N[:-1]
	N = "0" + N
	N = N[::-1]

	dp = [[inf,inf] for i in range(len(N)+2)]
	dp[0][0] = 0
	#print(dp)

	cnt = 0
	for ii in N:
		i = int(ii)
		cnt += 1
		#print(dp)
		for j in range(10):
			if i > j:
				tmp = j + 10 - i
				dp[cnt][1] = min(dp[cnt][1], dp[cnt-1][0] + tmp + j)
			else:
				tmp = j - i
				dp[cnt][0] = min(dp[cnt][0], dp[cnt-1][0] + tmp + j)

			tmptmp = j-1
			if i > tmptmp:
				tmp = tmptmp + 10 - i
				dp[cnt][1] = min(dp[cnt][1], dp[cnt-1][1] + tmp + j)
			else:
				tmp = tmptmp - i
				dp[cnt][0] = min(dp[cnt][0], dp[cnt-1][1] + tmp + j)

	#print(dp)
	print(dp[len(N)][0]) main N = input() N input() input N = N[:-1] N N[:-1] N :-1 -1 N = "0" + N N "0" + N "0" N N = N[::-1] N N[::-1] N ::-1 -1 [inf,inf] for i in range(len(N)+2) for i in range(len(N)+2) i range(len(N)+2) range len(N)+2 len(N) len N 2 for i in range(len(N)+2) [inf,inf] inf inf dp = [[inf,inf] for i in range(len(N)+2)] dp [[inf,inf] for i in range(len(N)+2)] dp[0][0] = 0 dp[0][0] [0] dp 0 0 0 cnt = 0 cnt 0 for ii in N:
		i = int(ii)
		cnt += 1
		#print(dp)
		for j in range(10):
			if i > j:
				tmp = j + 10 - i
				dp[cnt][1] = min(dp[cnt][1], dp[cnt-1][0] + tmp + j)
			else:
				tmp = j - i
				dp[cnt][0] = min(dp[cnt][0], dp[cnt-1][0] + tmp + j)

			tmptmp = j-1
			if i > tmptmp:
				tmp = tmptmp + 10 - i
				dp[cnt][1] = min(dp[cnt][1], dp[cnt-1][1] + tmp + j)
			else:
				tmp = tmptmp - i
				dp[cnt][0] = min(dp[cnt][0], dp[cnt-1][1] + tmp + j)

	#print(dp)
	 ii N i = int(ii) i int(ii) int ii cnt += 1 cnt 1 for j in range(10):
			if i > j:
				tmp = j + 10 - i
				dp[cnt][1] = min(dp[cnt][1], dp[cnt-1][0] + tmp + j)
			else:
				tmp = j - i
				dp[cnt][0] = min(dp[cnt][0], dp[cnt-1][0] + tmp + j)

			tmptmp = j-1
			if i > tmptmp:
				tmp = tmptmp + 10 - i
				dp[cnt][1] = min(dp[cnt][1], dp[cnt-1][1] + tmp + j)
			else:
				tmp = tmptmp - i
				dp[cnt][0] = min(dp[cnt][0], dp[cnt-1][1] + tmp + j)

	#print(dp)
	 j range(10) range 10 if i > j:
				tmp = j + 10 - i
				dp[cnt][1] = min(dp[cnt][1], dp[cnt-1][0] + tmp + j)
			else:
				tmp = j - i
				dp[cnt][0] = min(dp[cnt][0], dp[cnt-1][0] + tmp + j)

			 i > j i j tmp = j + 10 - i tmp j + 10 - i j + 10 j 10 i dp[cnt][1] = min(dp[cnt][1], dp[cnt-1][0] + tmp + j) dp[cnt][1] [cnt] dp cnt 1 min(dp[cnt][1], dp[cnt-1][0] + tmp + j) min dp[cnt][1] [cnt] dp cnt 1 dp[cnt-1][0] + tmp + j dp[cnt-1][0] + tmp dp[cnt-1][0] [cnt-1] dp cnt-1 cnt 1 0 tmp j tmp = j - i tmp j - i j i dp[cnt][0] = min(dp[cnt][0], dp[cnt-1][0] + tmp + j) dp[cnt][0] [cnt] dp cnt 0 min(dp[cnt][0], dp[cnt-1][0] + tmp + j) min dp[cnt][0] [cnt] dp cnt 0 dp[cnt-1][0] + tmp + j dp[cnt-1][0] + tmp dp[cnt-1][0] [cnt-1] dp cnt-1 cnt 1 0 tmp j tmptmp = j-1 tmptmp j-1 j 1 if i > tmptmp:
				tmp = tmptmp + 10 - i
				dp[cnt][1] = min(dp[cnt][1], dp[cnt-1][1] + tmp + j)
			else:
				tmp = tmptmp - i
				dp[cnt][0] = min(dp[cnt][0], dp[cnt-1][1] + tmp + j)

	#print(dp)
	 i > tmptmp i tmptmp tmp = tmptmp + 10 - i tmp tmptmp + 10 - i tmptmp + 10 tmptmp 10 i dp[cnt][1] = min(dp[cnt][1], dp[cnt-1][1] + tmp + j) dp[cnt][1] [cnt] dp cnt 1 min(dp[cnt][1], dp[cnt-1][1] + tmp + j) min dp[cnt][1] [cnt] dp cnt 1 dp[cnt-1][1] + tmp + j dp[cnt-1][1] + tmp dp[cnt-1][1] [cnt-1] dp cnt-1 cnt 1 1 tmp j tmp = tmptmp - i tmp tmptmp - i tmptmp i dp[cnt][0] = min(dp[cnt][0], dp[cnt-1][1] + tmp + j) dp[cnt][0] [cnt] dp cnt 0 min(dp[cnt][0], dp[cnt-1][1] + tmp + j) min dp[cnt][0] [cnt] dp cnt 0 dp[cnt-1][1] + tmp + j dp[cnt-1][1] + tmp dp[cnt-1][1] [cnt-1] dp cnt-1 cnt 1 1 tmp j print(dp[len(N)][0]) print dp[len(N)][0] [len(N)] dp len(N) len N 0 if __name__ == "__main__":
	main() __name__ == "__main__" __name__ "__main__" main() main inf = float('inf') float('inf') inf input = sys.stdin.readline sys.stdin.readline input def inputInt(): return int(input()) def inputInt(): return int(input()) inputInt def main():
	N = input()
	N = N[:-1]
	N = "0" + N
	N = N[::-1]

	dp = [[inf,inf] for i in range(len(N)+2)]
	dp[0][0] = 0
	#print(dp)

	cnt = 0
	for ii in N:
		i = int(ii)
		cnt += 1
		#print(dp)
		for j in range(10):
			if i > j:
				tmp = j + 10 - i
				dp[cnt][1] = min(dp[cnt][1], dp[cnt-1][0] + tmp + j)
			else:
				tmp = j - i
				dp[cnt][0] = min(dp[cnt][0], dp[cnt-1][0] + tmp + j)

			tmptmp = j-1
			if i > tmptmp:
				tmp = tmptmp + 10 - i
				dp[cnt][1] = min(dp[cnt][1], dp[cnt-1][1] + tmp + j)
			else:
				tmp = tmptmp - i
				dp[cnt][0] = min(dp[cnt][0], dp[cnt-1][1] + tmp + j)

	#print(dp)
	print(dp[len(N)][0]) def main():
	N = input()
	N = N[:-1]
	N = "0" + N
	N = N[::-1]

	dp = [[inf,inf] for i in range(len(N)+2)]
	dp[0][0] = 0
	#print(dp)

	cnt = 0
	for ii in N:
		i = int(ii)
		cnt += 1
		#print(dp)
		for j in range(10):
			if i > j:
				tmp = j + 10 - i
				dp[cnt][1] = min(dp[cnt][1], dp[cnt-1][0] + tmp + j)
			else:
				tmp = j - i
				dp[cnt][0] = min(dp[cnt][0], dp[cnt-1][0] + tmp + j)

			tmptmp = j-1
			if i > tmptmp:
				tmp = tmptmp + 10 - i
				dp[cnt][1] = min(dp[cnt][1], dp[cnt-1][1] + tmp + j)
			else:
				tmp = tmptmp - i
				dp[cnt][0] = min(dp[cnt][0], dp[cnt-1][1] + tmp + j)

	#print(dp)
	print(dp[len(N)][0]) main def inputMap(): return map(int, input().split()) def inputMap(): return map(int, input().split()) inputMap def inputList(): return list(map(int, input().split())) def inputList(): return list(map(int, input().split())) inputList
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

##### 無限 #####
# inf = float('inf')

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

inf = float('inf')

def main():
	N = input()
	N = N[:-1]
	N = "0" + N
	N = N[::-1]

	dp = [[inf,inf] for i in range(len(N)+2)]
	dp[0][0] = 0
	#print(dp)

	cnt = 0
	for ii in N:
		i = int(ii)
		cnt += 1
		#print(dp)
		for j in range(10):
			if i > j:
				tmp = j + 10 - i
				dp[cnt][1] = min(dp[cnt][1], dp[cnt-1][0] + tmp + j)
			else:
				tmp = j - i
				dp[cnt][0] = min(dp[cnt][0], dp[cnt-1][0] + tmp + j)

			tmptmp = j-1
			if i > tmptmp:
				tmp = tmptmp + 10 - i
				dp[cnt][1] = min(dp[cnt][1], dp[cnt-1][1] + tmp + j)
			else:
				tmp = tmptmp - i
				dp[cnt][0] = min(dp[cnt][0], dp[cnt-1][1] + tmp + j)

	#print(dp)
	print(dp[len(N)][0])

if __name__ == "__main__":
	main()
