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
import java.util.LinkedList;
import java.util.Scanner;


public class T440C {
	private long[] N = {
			1,
			11,
			111,
			1111,
			11111,
			111111,
			1111111,
			11111111,
			111111111,
			1111111111,
			11111111111L,
			111111111111L,
			1111111111111L,
			11111111111111L,
			111111111111111L,
			1111111111111111L
		};
	
	public long solve(long n) {
		long ans = Long.MAX_VALUE;
		LinkedList<long[]> l = new LinkedList<long[]>();
		l.add(new long[] {n, 0});
		long[] e;
		while (!l.isEmpty()) {
			e = l.getFirst();
			l.removeFirst();
			
			if (e[0] == 0 || e[1] > ans) {
				ans = Math.min(ans, e[1]);
				continue;
			}
			for (int i = N.length - 1; i >= 0; i --) {
				if (e[0] >= N[i]) {
					long x,y;

					y = e[1] + (e[0] / N[i]) *(i + 1);
					x = e[0] % N[i];
					l.add(new long[] {x, y});
					
					if (i < N.length - 1) {
						y = e[1] + (i + 1) + 1;
						x = N[i + 1] - e[0];
						l.add(new long[] {x, y});
						
					}
					break;
				}
			}
		}
		return ans;
	}
	public static void main(String[] args) {
		T440C t = new T440C();
		
		
		//long n = 973546235465729L;
		Scanner in = new Scanner(System.in);
		long n = in.nextLong();
		
		System.out.println(t.solve(n));
	}

}
