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 Rust (1.15.1) (judge verdict: Wrong Answer)

// 入力マクロ
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 より
#[macro_export]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}
#[macro_export]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}
#[macro_export]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

fn main() {
    input! {
        n: chars
    }
    let mut ans = 0;
    let mut v: Vec<u8> = n.into_iter().rev().map(|x| x.to_string().parse::<u8>().unwrap()).collect();
    // if !v.contains(&9) { ans += 1; }
    for i in 0..v.len()-1 {
        if v[i] == 9 {
            ans += 1;
            for j in &mut v[i..] {
                if *j == 9 { *j = 0; }
                else { *j += 1; }
            }
        } else if v[i] > 5 {
            ans += 10 - v[i];
            ans += 1;
        } else {
            ans += v[i];
        }
    }
    ans += v[v.len() - 1];
    println!("{}", ans);
}


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

use std::process;
pub fn exit() {
    process::exit(0)
}
/// 比較可能な2つの引数のうち大きい方を返す
pub fn max<T: PartialOrd>(n: T, m: T) -> T {
    if n >= m {
        n
    } else {
        m
    }
}

/// 比較可能な2つの引数のうち小さい方を返す
pub fn min<T: PartialOrd>(n: T, m: T) -> T {
    if n <= m {
        n
    } else {
        m
    }
}

/// floor + as i64
pub fn into_floor(a: f64) -> i64 {
    (a - a % 1.0) as i64
}

/// ceil + as i64
pub fn into_ceil(a: f64) -> i64 {
    (a - a % 1.0) as i64 + 1i64
}

/// "Yes", "No" を出力
pub fn yes_no(flag: bool) {
    if flag {
        println!("Yes");
    } else {
        println!("No");
    }
}

/// Yes
pub fn yes() {
    println!("Yes");
}

/// No
pub fn no() {
    println!("No");
}

/// 互除法を用いて最大公約数を求める
pub fn gcd(n: u64, m: u64) -> u64 {
    let (mut n, mut m) = (max(n, m), min(n, m));
    if m == 0 { return n; }
    let mut _r = 0;
    while n % m != 0 {
        _r = n % m;
        n = m;
        m = _r;
    }
    m
}

/// 最小公倍数を求める
pub fn lcm(n: u64, m: u64) -> u64 {
    n * m / gcd(n, m)
}

/// 配列全体の最大公約数
pub fn gcd_vec(v: &[u64]) -> u64 {
    let mut r = v[0];
    for i in v.iter().skip(1) {
        r = gcd(r, *i);
    }
    r
}

/// 配列全体の最小公倍数
pub fn lcm_vec(v: &[u64]) -> u64 {
    let mut r = v[0];
    for i in v.iter().skip(1) {
        r = lcm(r, *i);
    }
    r
}

/// Find the first factor (other than 1) of a number
fn firstfac(x: u64) -> u64 {
    if x % 2 == 0 {
        return 2;
    };
    // TODO: return to step_by
    // for n in (3..).step_by(2).take_while(|m| m*m <= x) {
    for n in (1..).map(|m| 2 * m + 1).take_while(|m| m * m <= x) {
        if x % n == 0 {
            return n;
        };
    }
    // No factor found. It must be prime.
    x
}

/// Test whether a number is prime. Checks every odd number up to `sqrt(n)`.
pub fn is_prime(n: u64) -> bool {
    if n <= 1 {
        return false;
    }
    firstfac(n) == n
}

// TODO!
// pub fn parmutation<T: PartialOrd>(v: &Vec<T>) -> Vec<Vec<T>> {
//     let mut sorted = v;

// }