Prof. Vasechkin wants to represent positive integer *n* as a sum of addends, where each addends is an integer number containing only 1s. For example, he can represent 121 as 121=111+11+–1. Help him to find the least number of digits 1 in such sum.
The first line of the input contains integer *n* (1 ≤ *n* &lt; 10<sup class="upper-index">15</sup>).
n = int(input())
v = []
v.append(1)
for i in range(1, 16):
	x = v[-1]
	x *= 10
	x = x+1
	v.append(x)
w = []
for x in v:
	w.append(-x)
now = 0
ans = 0
while now != n:
	mn = abs(n-now-1)
	new = 1
	use = 1
	for x in v:
		if abs(n-(now+x)) < mn or (abs(n-(now+x)) == mn and len(str(x)) < new):
			mn = abs(n-(now+x))
			new = len(str(x))
			use = x
	for x in w:
		if abs(n-(now+x)) < mn or (abs(n-(now+x)) == mn and len(str(x))-1 < new):
			mn = abs(n-(now+x))
			new = len(str(x))-1
			use = x
	now += use
	ans += new
print(ans)