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.

Input

The first line of the input contains integer n (1 ≤ n < 1015).

Output

Print expected minimal number of digits 1.

Examples

Input

121


Output

6
def solve(n,p):
	aparitii = n/a[p];
	if(n%a[p]==0):
		return aparitii*p;
	sol1 = aparitii * p + solve(n%a[p],p-1);
	sol2 = (aparitii+1) * p + solve(a[p]*(aparitii+1)-n,p-1);
	return min(sol1,sol2);
		

a = [];
n = int(input());

a.append(0);
for i in range(1,18):
	a.append(a[i-1]*10+1);

print solve(n,16)
