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
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 *
 * @author sousnake
 */
public class C440_cpy {
    static long arr[];
    public static void main(String[] args) throws IOException {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            long n = Long.parseLong(br.readLine());
            arr = new long[20];
            for(int i=1;i<=16;i++){
                arr[i]=arr[i-1]*10+1;
            }
            System.out.println(fuc(n,16));
    }
    public static long fuc(long n, int d){
        long q =(n/arr[d]*d);
        n %=arr[d];
        if (n==0)
            return q;
        else return q+Math.min(d+fuc(arr[d]-n,d-1),fuc(n,d-1));
    }
}
