import sys
import numpy as np



def main(N,dp):
    dp[0, 0] = min(N[0], 11 - N[0])
    dp[0, 1] = min(N[0] + 1, 10 - N[0])
    for i in range(1, len(N)):
        dp[i, 0] = min(dp[i - 1, 0] + N[i], dp[i - 1, 1] + 10 - N[i])
        dp[i, 1] = min(dp[i - 1, 0] + N[i] + 1, dp[i - 1, 1] + 9 - N[i])
    print(dp[-1,0])


if sys.argv[-1] == 'ONLINE_JUDGE':
    import numba
    from numba.pycc import CC
    cc = CC('my_module')
    cc.export('main', '(i8[:],i8[:,:])')(main)
    # b1: bool, i4: int32, i8: int64, double: f8, [:], [:, :]
    cc.compile()
else:
    from my_module import main
    N = np.array([int(_) for _ in input()])
    dp=np.zeros((len(N),2),dtype=np.int64)
    main(N,dp) import sys sys import numpy as np numpy def main(N,dp):
    dp[0, 0] = min(N[0], 11 - N[0])
    dp[0, 1] = min(N[0] + 1, 10 - N[0])
    for i in range(1, len(N)):
        dp[i, 0] = min(dp[i - 1, 0] + N[i], dp[i - 1, 1] + 10 - N[i])
        dp[i, 1] = min(dp[i - 1, 0] + N[i] + 1, dp[i - 1, 1] + 9 - N[i])
    print(dp[-1,0]) main dp[0, 0] = min(N[0], 11 - N[0]) dp[0, 0] dp 0 min(N[0], 11 - N[0]) min N[0] N 0 11 - N[0] 11 N[0] N 0 dp[0, 1] = min(N[0] + 1, 10 - N[0]) dp[0, 1] dp 0 min(N[0] + 1, 10 - N[0]) min N[0] + 1 N[0] N 0 1 10 - N[0] 10 N[0] N 0 for i in range(1, len(N)):
        dp[i, 0] = min(dp[i - 1, 0] + N[i], dp[i - 1, 1] + 10 - N[i])
        dp[i, 1] = min(dp[i - 1, 0] + N[i] + 1, dp[i - 1, 1] + 9 - N[i])
     i range(1, len(N)) range 1 len(N) len N dp[i, 0] = min(dp[i - 1, 0] + N[i], dp[i - 1, 1] + 10 - N[i]) dp[i, 0] dp i min(dp[i - 1, 0] + N[i], dp[i - 1, 1] + 10 - N[i]) min dp[i - 1, 0] + N[i] dp[i - 1, 0] dp i - 1 i 1 N[i] N i dp[i - 1, 1] + 10 - N[i] dp[i - 1, 1] + 10 dp[i - 1, 1] dp i - 1 i 1 10 N[i] N i dp[i, 1] = min(dp[i - 1, 0] + N[i] + 1, dp[i - 1, 1] + 9 - N[i]) dp[i, 1] dp i min(dp[i - 1, 0] + N[i] + 1, dp[i - 1, 1] + 9 - N[i]) min dp[i - 1, 0] + N[i] + 1 dp[i - 1, 0] + N[i] dp[i - 1, 0] dp i - 1 i 1 N[i] N i 1 dp[i - 1, 1] + 9 - N[i] dp[i - 1, 1] + 9 dp[i - 1, 1] dp i - 1 i 1 9 N[i] N i print(dp[-1,0]) print dp[-1,0] dp -1 N N dp dp if sys.argv[-1] == 'ONLINE_JUDGE':
    import numba
    from numba.pycc import CC
    cc = CC('my_module')
    cc.export('main', '(i8[:],i8[:,:])')(main)
    # b1: bool, i4: int32, i8: int64, double: f8, [:], [:, :]
    cc.compile()
else:
    from my_module import main
    N = np.array([int(_) for _ in input()])
    dp=np.zeros((len(N),2),dtype=np.int64)
    main(N,dp) sys.argv[-1] == 'ONLINE_JUDGE' sys.argv[-1] sys.argv sys argv -1 'ONLINE_JUDGE' import numba numba from numba.pycc import CC cc = CC('my_module') cc CC('my_module') CC 'my_module' cc.export('main', '(i8[:],i8[:,:])')(main) cc.export('main', '(i8[:],i8[:,:])') cc.export cc export 'main' '(i8[:],i8[:,:])' main cc.compile() cc.compile cc compile from my_module import main int(_) for _ in input() for _ in input() _ input() input for _ in input() int(_) int _ N = np.array([int(_) for _ in input()]) N np.array([int(_) for _ in input()]) np.array np array [int(_) for _ in input()] dp=np.zeros((len(N),2),dtype=np.int64) dp np.zeros((len(N),2),dtype=np.int64) np.zeros np zeros (len(N),2) len(N) len N 2 dtype=np.int64 dtype np.int64 np int64 main(N,dp) main N dp def main(N,dp):
    dp[0, 0] = min(N[0], 11 - N[0])
    dp[0, 1] = min(N[0] + 1, 10 - N[0])
    for i in range(1, len(N)):
        dp[i, 0] = min(dp[i - 1, 0] + N[i], dp[i - 1, 1] + 10 - N[i])
        dp[i, 1] = min(dp[i - 1, 0] + N[i] + 1, dp[i - 1, 1] + 9 - N[i])
    print(dp[-1,0]) def main(N,dp):
    dp[0, 0] = min(N[0], 11 - N[0])
    dp[0, 1] = min(N[0] + 1, 10 - N[0])
    for i in range(1, len(N)):
        dp[i, 0] = min(dp[i - 1, 0] + N[i], dp[i - 1, 1] + 10 - N[i])
        dp[i, 1] = min(dp[i - 1, 0] + N[i] + 1, dp[i - 1, 1] + 9 - N[i])
    print(dp[-1,0]) main cc = CC('my_module') CC('my_module') cc N = np.array([int(_) for _ in input()]) np.array([int(_) for _ in input()]) N dp=np.zeros((len(N),2),dtype=np.int64) np.zeros((len(N),2),dtype=np.int64) dp
import sys
import numpy as np



def main(N,dp):
    dp[0, 0] = min(N[0], 11 - N[0])
    dp[0, 1] = min(N[0] + 1, 10 - N[0])
    for i in range(1, len(N)):
        dp[i, 0] = min(dp[i - 1, 0] + N[i], dp[i - 1, 1] + 10 - N[i])
        dp[i, 1] = min(dp[i - 1, 0] + N[i] + 1, dp[i - 1, 1] + 9 - N[i])
    print(dp[-1,0])


if sys.argv[-1] == 'ONLINE_JUDGE':
    import numba
    from numba.pycc import CC
    cc = CC('my_module')
    cc.export('main', '(i8[:],i8[:,:])')(main)
    # b1: bool, i4: int32, i8: int64, double: f8, [:], [:, :]
    cc.compile()
else:
    from my_module import main
    N = np.array([int(_) for _ in input()])
    dp=np.zeros((len(N),2),dtype=np.int64)
    main(N,dp)
