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.InputStreamReader;
import java.io.IOException;
import java.util.HashMap;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Vadim
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskC solver = new TaskC();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskC {
    long a[] = new long[17];
    HashMap<Long, Long> cache = new HashMap<Long, Long>();

    private long calc(long n) {
        if(n==0) {
            return 0;
        }
        if(cache.containsKey(n)){
            return cache.get(n);
        }
        for (int i = 0; i < 17; i++) {
            if(a[i] >= n) {
                if(a[i] == n) {
                    cache.put(n, (long)i+1);
                    return i+1;
                }

                long k = n/a[i-1];
                long c1 = i*k + calc(n-a[i - 1]*k);

                k = (a[i]-n)/a[i-1];
                long c2 = i + 1 + i*k+ calc(a[i]-a[i-1]*k-n);

                long min = Math.min(c1, c2);
                cache.put(n, min);
                return min;
            }
        }
        throw new RuntimeException();
    }

    public void solve(int testNumber, InputReader in, PrintWriter out) {
        long n = in.nextLong();

        a[0] = 1;
        for (int i = 1; i < 17; i++) {
            a[i] = a[i-1]*10 + 1;
        }

        out.println(calc(n));

    }
}

class InputReader  extends BufferedReader {
    public InputReader(InputStream st) {
        super(new InputStreamReader(st));
    }

    public long nextLong() {
        try {
            int c = read();

            while (isWhiteSpace(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long ret = 0;
            while (c >= 0 && !isWhiteSpace(c)) {
                if (c < '0' || c > '9') {
                    throw new RuntimeException();
                }
                ret = ret * 10 + c - '0';
                c = read();
            }
            return ret * sgn;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public String readLine() {
        try {
            return super.readLine();
        } catch (IOException e) {
            return null;
        }
    }

    static boolean isWhiteSpace(int c) {
        return c >= 0 && c <= 32;
    }



}
