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.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
/**
 121 = 11 + 11 + 11 + 11 
 81924761239462
 * */
public class C_Round_10 {
	public static void main(String[]arg) throws IOException{
		new C_Round_10().read();
	}	
	BigInteger[]bs;
	int[]lengths;
	boolean same;
	public void read()throws IOException{
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		fill();
		BigInteger n = new BigInteger(in.readLine());
		long ans = getMin(n);
		System.out.println(ans);
	}
	private void fill(){
		bs = new BigInteger[16];
		lengths = new int[16];
		String one = "1";  
		for (int i = 0; i < bs.length; i++) {
			bs[i] = new BigInteger(one);
			lengths[i] = i+1;
			one += '1';
			
		}
	}
	public long getMin(BigInteger n){
		long ans = Long.MAX_VALUE,a,b;
		BigInteger nn,m;
		//System.out.println(n);
		int ind;
		if(n.compareTo(BigInteger.ZERO) == 0) ans = 0;
		else{
			ind = getP(n);
			a = b = Long.MAX_VALUE;
			if(ind - 1 > -1){
				m = n.divide(bs[ind-1]);
				if(m.compareTo(BigInteger.ZERO) == 0)
					m = BigInteger.ONE;
				nn = n.subtract(m.multiply(bs[ind-1]));
				nn = abs(nn);
				//System.out.println(m.intValue());
				a = lengths[ind-1]*m.intValue() + getMin(nn);
			}
			m = n.divide(bs[ind]);
			if(m.compareTo(BigInteger.ZERO) == 0)
				m = BigInteger.ONE;
			nn = n.subtract(m.multiply(bs[ind]));
			nn = abs(nn);
			b = lengths[ind]*m.intValue() + getMin(nn);
			ans = Math.min(a, b);
		}
		return ans;
	}
	private long getSum(BigInteger n){
		long ans = 0;
		int ind;
		BigInteger m;
		while(n.compareTo(BigInteger.ZERO) != 0){
			ind = getP(n);
			//System.out.println(bs[ind]);
			/*
			if(bs[ind].compareTo(n) == 0){
				ans += 2 + lengths[ind];
				n = BigInteger.ZERO;
			}else{
				ans += lengths[ind];
				n = n.subtract(bs[ind]);
				n = abs(n);
			}*/
			
			
			m = n.divide(bs[ind]);
			//System.out.println(bs[ind]);
			if(m.compareTo(BigInteger.ZERO) == 0)
				m = BigInteger.ONE;
			ans += lengths[ind]*m.intValue();
			n = n.subtract(m.multiply(bs[ind]));
			n = abs(n);
		}
		return ans;
	}
	private int getP(BigInteger b){
		int l = 0;
		for (int i = 0; i < bs.length; i++) {
			if(b.compareTo(bs[i]) <= 0){
				l = i;
				break;
			}
		}
		if(l - 1 > -1){
			BigInteger x = bs[l].subtract(b),u,v;
			BigInteger y = bs[l-1].subtract(b);
			x = abs(x); y = abs(y);
			if(x.compareTo(y) >= 0){
				l = l -1;
			}
		}
		return l;
	}
	private BigInteger abs(BigInteger a){
		BigInteger one = new BigInteger("-1"),b;
		b = a;
		if(a.compareTo(BigInteger.ZERO) < 0)
			b = a.multiply(one);
		return b;
	}
}
