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.Scanner;

public class Main {
    static long [] en=new long[] {1, 11, 111, 1111, 11111, 111111, 1111111, 11111111, 111111111, 1111111111,
            11111111111L, 111111111111L, 1111111111111L, 11111111111111L, 111111111111111L, 1111111111111111L,11111111111111111L};



    public static void main(String[] args) {
        System.out.println(solve(new Scanner(System.in).nextLong()));
    }
    static long solve(long s){
        if(s<=11){
            return Math.min(s, 2+11-s);
        }
        int dolzina = (int) (Math.log10(s)+1);
        if (Math.abs((en[dolzina])-s)<s ){
            return (Math.min((solve(Math.abs(s-en[dolzina-1]))+ dolzina),(solve((en[dolzina])-s))+(dolzina+1)));
        }

        return solve(Math.abs(s-en[dolzina-1]))+ dolzina;
    }

}