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 {
    public static void main(String[] args) {
        Scanner myObj = new Scanner(System.in);
        long n = myObj.nextLong();
        long[] enke = new long[17];
        for (int i = 1; i < 17; ++i) {
            enke[i] = 10 * enke[i - 1] + 1; //število enic na vsaki stopnji
        }
        long ans = deli(enke, n, 16);
        System.out.println(ans);
    }

    private static long deli(long[] enke, long n, int id) {
        long kvota = n / enke[id];
        n %= enke[id]; //koliko se manjka do do števila
        long onesUsedHere = kvota * id;
        if (n == 0) return onesUsedHere;
        else
            return onesUsedHere + Math.min(deli(enke, n, id - 1),
                                    id + deli(enke, enke[id] - n, id - 1)); //najdemo najbližjo cifro
    }

}