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

public class C {
    static int inf = (int) 1e6;
    static int[][][][] DP;
    static String number;

    public static int get(int index, int pos, int neg, int carry) {
        int truCarry = carry - 50;
        if (index == 17) {
            if (pos + neg + truCarry == 0) {
                return 0;
            } else {
                return inf;
            }
        }
        if (DP[index][pos][neg][carry] != -1)
            return DP[index][pos][neg][carry];
        int res = inf;
        for (int p = 0; p <= Math.min(15, pos); p++)
            for (int n = 0; n <= Math.min(15, neg); n++) {
                int sum = pos - neg + truCarry;
                int currentBit = (sum % 10 + 10) % 10;
                int nextCarry = sum > 0 ? (sum / 10) : ((sum - 9) / 10);
                if (currentBit != number.charAt(index) - '0')
                    continue;
                res = Math.min(res,
                        get(index + 1, pos - p, neg - n, nextCarry + 50)
                                + (p + n) * (index + 1));
            }
        return DP[index][pos][neg][carry] = res;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        long n = in.nextLong();
        int size = 50;
        DP = new int[20][size][size][100];
        for (int i = 0; i < 20; i++)
            for (int j = 0; j < size; j++)
                for (int k = 0; k < size; k++)
                    Arrays.fill(DP[i][j][k], -1);
        number = "" + n;
        while (number.length() < 17)
            number = "0" + number;
        number = new StringBuffer(number).reverse().toString();
        int res = inf;
        for (int pos = 0; pos < size; pos++)
            for (int neg = 0; neg < size; neg++)
                res = Math.min(res, get(0, pos, neg, 50));
        System.out.println(res);
    }
}
