import sys

def resolve(in_):
    dp0 = [1 << 60] * 1000100
    dp1 = [1 << 60] * 1000100
    zero = ord(b'0')
    dp0[0] = 0
    dp1[0] = 2

    for i, v in enumerate(in_.read().rstrip()):
        v -= zero

        # ちょうど支払う。おつりなし。
        dp0[i + 1] = min(dp0[i] + v, dp1[i] + v)

        # 前の桁の支払い枚数に 1 を加えてお釣りを払うのと、
        # 前の桁で多く払ってあるのでお釣りの払い出しの継続の二通り
        dp1[i + 1] = min(dp0[i] + 1 + 10 - v, dp1[i] + 9 - v)
    else:
        i += 1


    return min(dp0[i], dp1[i])


def main():
    answer = resolve(sys.stdin.buffer)
    print(answer)


if __name__ == '__main__':
    main() import sys sys def resolve(in_):
    dp0 = [1 << 60] * 1000100
    dp1 = [1 << 60] * 1000100
    zero = ord(b'0')
    dp0[0] = 0
    dp1[0] = 2

    for i, v in enumerate(in_.read().rstrip()):
        v -= zero

        # ちょうど支払う。おつりなし。
        dp0[i + 1] = min(dp0[i] + v, dp1[i] + v)

        # 前の桁の支払い枚数に 1 を加えてお釣りを払うのと、
        # 前の桁で多く払ってあるのでお釣りの払い出しの継続の二通り
        dp1[i + 1] = min(dp0[i] + 1 + 10 - v, dp1[i] + 9 - v)
    else:
        i += 1


    return min(dp0[i], dp1[i]) resolve dp0 = [1 << 60] * 1000100 dp0 [1 << 60] * 1000100 [1 << 60] 1 << 60 1 60 1000100 dp1 = [1 << 60] * 1000100 dp1 [1 << 60] * 1000100 [1 << 60] 1 << 60 1 60 1000100 zero = ord(b'0') zero ord(b'0') ord b'0' dp0[0] = 0 dp0[0] dp0 0 0 dp1[0] = 2 dp1[0] dp1 0 2 for i, v in enumerate(in_.read().rstrip()):
        v -= zero

        # ちょうど支払う。おつりなし。
        dp0[i + 1] = min(dp0[i] + v, dp1[i] + v)

        # 前の桁の支払い枚数に 1 を加えてお釣りを払うのと、
        # 前の桁で多く払ってあるのでお釣りの払い出しの継続の二通り
        dp1[i + 1] = min(dp0[i] + 1 + 10 - v, dp1[i] + 9 - v)
    else:
        i += 1


     i v enumerate(in_.read().rstrip()) enumerate in_.read().rstrip() in_.read().rstrip in_.read() in_.read in_ read rstrip v -= zero v zero dp0[i + 1] = min(dp0[i] + v, dp1[i] + v) dp0[i + 1] dp0 i + 1 i 1 min(dp0[i] + v, dp1[i] + v) min dp0[i] + v dp0[i] dp0 i v dp1[i] + v dp1[i] dp1 i v dp1[i + 1] = min(dp0[i] + 1 + 10 - v, dp1[i] + 9 - v) dp1[i + 1] dp1 i + 1 i 1 min(dp0[i] + 1 + 10 - v, dp1[i] + 9 - v) min dp0[i] + 1 + 10 - v dp0[i] + 1 + 10 dp0[i] + 1 dp0[i] dp0 i 1 10 v dp1[i] + 9 - v dp1[i] + 9 dp1[i] dp1 i 9 v i += 1 i 1 return min(dp0[i], dp1[i]) min(dp0[i], dp1[i]) min dp0[i] dp0 i dp1[i] dp1 i in_ in_ def main():
    answer = resolve(sys.stdin.buffer)
    print(answer) main answer = resolve(sys.stdin.buffer) answer resolve(sys.stdin.buffer) resolve sys.stdin.buffer sys.stdin sys stdin buffer print(answer) print answer if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def resolve(in_):
    dp0 = [1 << 60] * 1000100
    dp1 = [1 << 60] * 1000100
    zero = ord(b'0')
    dp0[0] = 0
    dp1[0] = 2

    for i, v in enumerate(in_.read().rstrip()):
        v -= zero

        # ちょうど支払う。おつりなし。
        dp0[i + 1] = min(dp0[i] + v, dp1[i] + v)

        # 前の桁の支払い枚数に 1 を加えてお釣りを払うのと、
        # 前の桁で多く払ってあるのでお釣りの払い出しの継続の二通り
        dp1[i + 1] = min(dp0[i] + 1 + 10 - v, dp1[i] + 9 - v)
    else:
        i += 1


    return min(dp0[i], dp1[i]) def resolve(in_):
    dp0 = [1 << 60] * 1000100
    dp1 = [1 << 60] * 1000100
    zero = ord(b'0')
    dp0[0] = 0
    dp1[0] = 2

    for i, v in enumerate(in_.read().rstrip()):
        v -= zero

        # ちょうど支払う。おつりなし。
        dp0[i + 1] = min(dp0[i] + v, dp1[i] + v)

        # 前の桁の支払い枚数に 1 を加えてお釣りを払うのと、
        # 前の桁で多く払ってあるのでお釣りの払い出しの継続の二通り
        dp1[i + 1] = min(dp0[i] + 1 + 10 - v, dp1[i] + 9 - v)
    else:
        i += 1


    return min(dp0[i], dp1[i]) resolve def main():
    answer = resolve(sys.stdin.buffer)
    print(answer) def main():
    answer = resolve(sys.stdin.buffer)
    print(answer) main
import sys

def resolve(in_):
    dp0 = [1 << 60] * 1000100
    dp1 = [1 << 60] * 1000100
    zero = ord(b'0')
    dp0[0] = 0
    dp1[0] = 2

    for i, v in enumerate(in_.read().rstrip()):
        v -= zero

        # ちょうど支払う。おつりなし。
        dp0[i + 1] = min(dp0[i] + v, dp1[i] + v)

        # 前の桁の支払い枚数に 1 を加えてお釣りを払うのと、
        # 前の桁で多く払ってあるのでお釣りの払い出しの継続の二通り
        dp1[i + 1] = min(dp0[i] + 1 + 10 - v, dp1[i] + 9 - v)
    else:
        i += 1


    return min(dp0[i], dp1[i])


def main():
    answer = resolve(sys.stdin.buffer)
    print(answer)


if __name__ == '__main__':
    main()
