Problem:
Score: 500 points

Problem Statement

In the Kingdom of AtCoder, only banknotes are used as currency. There are 10^{100}+1 kinds of banknotes, with the values of 1, 10, 10^2, 10^3, \dots, 10^{(10^{100})}. You have come shopping at a mall and are now buying a takoyaki machine with a value of N. (Takoyaki is the name of a Japanese snack.)

To make the payment, you will choose some amount of money which is at least N and give it to the clerk. Then, the clerk gives you back the change, which is the amount of money you give minus N.

What will be the minimum possible number of total banknotes used by you and the clerk, when both choose the combination of banknotes to minimize this count?

Assume that you have sufficient numbers of banknotes, and so does the clerk.

Constraints

N is an integer between 1 and 10^{1,000,000} (inclusive).

Input

Input is given from Standard Input in the following format:

N

Output

Print the minimum possible number of total banknotes used by you and the clerk.

Sample Input 1

36

Sample Output 1

8

If you give four banknotes of value 10 each, and the clerk gives you back four banknotes of value 1 each, a total of eight banknotes are used.

The payment cannot be made with less than eight banknotes in total, so the answer is 8.

Sample Input 2

91

Sample Output 2

3

If you give two banknotes of value 100, 1, and the clerk gives you back one banknote of value 10, a total of three banknotes are used.

Sample Input 3

314159265358979323846264338327950288419716939937551058209749445923078164062862089986280348253421170

Sample Output 3

243

Solution in Go (1.6) (judge verdict: Runtime Error)

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

var sc *bufio.Scanner

const BufferSize = 1024

func nextString() string {
	if !sc.Scan() {
		panic(nil)
	}
	return sc.Text()
}

func nextInt() int {
	if !sc.Scan() {
		panic(nil)
	}
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

var production bool

func debugf(format string, v ...interface{}) {
	if !production {
		fmt.Printf(format, v...)
	}
}

func solve(s []int, i int, o int) int {
	if i == 0 {
		return s[i] + o
	}
	if s[i]+o < 5 {
		ans := solve(s, i-1, 0)
		return ans + s[i] + o
	}
	if s[i]+o > 5 {
		//s[i] = 10 - (s[i] + o)
		ans := solve(s, i-1, 1)
		return ans + (10 - (s[i] + o))
	}
	a1 := solve(s, i-1, 0)
	a2 := solve(s, i-1, 1)
	//a2 := a1
	if a1+s[i]+o < a2+(10-(s[i]+o)) {
		return a1 + s[i] + o
	}
	return a2 + (10 - s[i] + o)
}

func answer(reader io.Reader, writer io.Writer) {
	sc = bufio.NewScanner(reader)
	buf := make([]byte, BufferSize)
	sc.Buffer(buf, 1e+6)
	sc.Split(bufio.ScanWords)
	n := nextString()
	//debugf("%v\n", n)
	s := make([]int, len(n)+1)
	for i := 0; i < len(n); i++ {
		d := int(n[i] - '0')
		s[i+1] = d
	}
	ans := solve(s, len(s)-1, 0)
	//debugf("%v\n", s)
	_, _ = fmt.Fprintf(writer, "%d", ans)
}

func answer2(reader io.Reader, writer io.Writer) {
	sc = bufio.NewScanner(reader)
	buf := make([]byte, BufferSize)
	sc.Buffer(buf, 1e+6)
	sc.Split(bufio.ScanWords)
	n := nextString()
	//debugf("%v\n", n)
	s := make([]int, len(n)+1)
	a := make([]int, len(n)+1)
	b := make([]int, len(n)+1)
	for i := 0; i < len(n); i++ {
		d := int(n[i] - '0')
		s[i+1] = d
		a[i+1] = d
	}
	for i := len(s) - 1; i > 0; i-- {
		switch {
		case s[i] == 10:
			s[i] = 0
			s[i-1]++
		case s[i] > 5:
			s[i-1]++
			s[i] = 10 - s[i]
			b[i] = s[i]
		case s[i] == 5 && i > 0:
			for j := 1; i-j > 0; j++ {
				if s[i-j] > 5 {
					for k := 0; k < j; k++ {
						s[i-k-1]++
						s[i-k] = 10 - s[i-k]
						b[i-k] = s[i-k]

					}
					i = i - j + 1
					break
				}
				if s[i-j] < 5 {
					break
				}
			}
		}
	}

	//for i := range s {
	//	debugf("i:%d a:%d s:%d, b:%d\n", i, a[i], s[i]-b[i], b[i])
	//}
	ans := 0
	for i := range s {
		ans += s[i]
	}
	_, _ = fmt.Fprintf(writer, "%d", ans)
}

func main() {
	production = true
	answer(os.Stdin, os.Stdout)
}