def s_in():
    return input()

def n_in():
    return int(input())

def l_in():
    return list(map(int, input().split()))

class Interval():
    def __init__(self, li):
        self.li = li
        self.n = len(li)
        self.sum_li = [li[0]]
        for i in range(1, self.n):
            self.sum_li.append(self.sum_li[i-1] + li[i])

    def sum(self, a, b=None):
        if b is None:
            return self.sum(0, a)

        res = self.sum_li[min(self.n-1, b-1)]
        if a > 0:
            res -= self.sum_li[a-1]
        return res

N = s_in()[::-1]
n = len(N)

dp1 = [0 for _ in range(n)]
dp1[0] = int(N[0])
# dp1[i] は i桁目を同じ値だけ使ったとしたときの最小値
dp2 = [0 for _ in range(n)]
dp2[0] = 10 - int(N[0])
# dp2[i] は i桁目を繰り上げたとしたときの最小値

for i, s in enumerate(N[1:]):
    i += 1

    m = int(s)
    dp1[i] = min(dp1[i-1] + m,      dp2[i-1] + (m+1))
    dp2[i] = min(dp1[i-1] + (10-m), dp2[i-1] + (10-m-1))

# print(dp1)
# print( dp2)
print(min(dp1[n-1], dp2[n-1]+1)) def s_in():
    return input() s_in return input() input() input def n_in():
    return int(input()) n_in return int(input()) int(input()) int input() input def l_in():
    return list(map(int, input().split())) l_in return list(map(int, input().split())) list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split class Interval():
    def __init__(self, li):
        self.li = li
        self.n = len(li)
        self.sum_li = [li[0]]
        for i in range(1, self.n):
            self.sum_li.append(self.sum_li[i-1] + li[i])

    def sum(self, a, b=None):
        if b is None:
            return self.sum(0, a)

        res = self.sum_li[min(self.n-1, b-1)]
        if a > 0:
            res -= self.sum_li[a-1]
        return res Interval def __init__(self, li):
        self.li = li
        self.n = len(li)
        self.sum_li = [li[0]]
        for i in range(1, self.n):
            self.sum_li.append(self.sum_li[i-1] + li[i])

     __init__ self.li = li self.li self li li self.n = len(li) self.n self n len(li) len li self.sum_li = [li[0]] self.sum_li self sum_li [li[0]] li[0] li 0 for i in range(1, self.n):
            self.sum_li.append(self.sum_li[i-1] + li[i])

     i range(1, self.n) range 1 self.n self n self.sum_li.append(self.sum_li[i-1] + li[i]) self.sum_li.append self.sum_li self sum_li append self.sum_li[i-1] + li[i] self.sum_li[i-1] self.sum_li self sum_li i-1 i 1 li[i] li i self self li li def sum(self, a, b=None):
        if b is None:
            return self.sum(0, a)

        res = self.sum_li[min(self.n-1, b-1)]
        if a > 0:
            res -= self.sum_li[a-1]
        return res sum if b is None:
            return self.sum(0, a)

         b is None b None return self.sum(0, a) self.sum(0, a) self.sum self sum 0 a res = self.sum_li[min(self.n-1, b-1)] res self.sum_li[min(self.n-1, b-1)] self.sum_li self sum_li min(self.n-1, b-1) min self.n-1 self.n self n 1 b-1 b 1 if a > 0:
            res -= self.sum_li[a-1]
         a > 0 a 0 res -= self.sum_li[a-1] res self.sum_li[a-1] self.sum_li self sum_li a-1 a 1 return res res self self a a b=None b None N = s_in()[::-1] N s_in()[::-1] () s_in ::-1 -1 n = len(N) n len(N) len N 0 for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) 0 dp1 = [0 for _ in range(n)] dp1 [0 for _ in range(n)] dp1[0] = int(N[0]) dp1[0] dp1 0 int(N[0]) int N[0] N 0 0 for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) 0 dp2 = [0 for _ in range(n)] dp2 [0 for _ in range(n)] dp2[0] = 10 - int(N[0]) dp2[0] dp2 0 10 - int(N[0]) 10 int(N[0]) int N[0] N 0 for i, s in enumerate(N[1:]):
    i += 1

    m = int(s)
    dp1[i] = min(dp1[i-1] + m,      dp2[i-1] + (m+1))
    dp2[i] = min(dp1[i-1] + (10-m), dp2[i-1] + (10-m-1))

# print(dp1)
# print( dp2) i s enumerate(N[1:]) enumerate N[1:] N 1: 1 i += 1 i 1 m = int(s) m int(s) int s dp1[i] = min(dp1[i-1] + m,      dp2[i-1] + (m+1)) dp1[i] dp1 i min(dp1[i-1] + m,      dp2[i-1] + (m+1)) min dp1[i-1] + m dp1[i-1] dp1 i-1 i 1 m dp2[i-1] + (m+1) dp2[i-1] dp2 i-1 i 1 m+1 m 1 dp2[i] = min(dp1[i-1] + (10-m), dp2[i-1] + (10-m-1)) dp2[i] dp2 i min(dp1[i-1] + (10-m), dp2[i-1] + (10-m-1)) min dp1[i-1] + (10-m) dp1[i-1] dp1 i-1 i 1 10-m 10 m dp2[i-1] + (10-m-1) dp2[i-1] dp2 i-1 i 1 10-m-1 10-m 10 m 1 print(min(dp1[n-1], dp2[n-1]+1)) print min(dp1[n-1], dp2[n-1]+1) min dp1[n-1] dp1 n-1 n 1 dp2[n-1]+1 dp2[n-1] dp2 n-1 n 1 1 def n_in():
    return int(input()) def n_in():
    return int(input()) n_in dp2 = [0 for _ in range(n)] [0 for _ in range(n)] dp2 n = len(N) len(N) n N = s_in()[::-1] s_in()[::-1] N dp1 = [0 for _ in range(n)] [0 for _ in range(n)] dp1 def l_in():
    return list(map(int, input().split())) def l_in():
    return list(map(int, input().split())) l_in m = int(s) int(s) m class Interval():
    def __init__(self, li):
        self.li = li
        self.n = len(li)
        self.sum_li = [li[0]]
        for i in range(1, self.n):
            self.sum_li.append(self.sum_li[i-1] + li[i])

    def sum(self, a, b=None):
        if b is None:
            return self.sum(0, a)

        res = self.sum_li[min(self.n-1, b-1)]
        if a > 0:
            res -= self.sum_li[a-1]
        return res class Interval():
    def __init__(self, li):
        self.li = li
        self.n = len(li)
        self.sum_li = [li[0]]
        for i in range(1, self.n):
            self.sum_li.append(self.sum_li[i-1] + li[i])

    def sum(self, a, b=None):
        if b is None:
            return self.sum(0, a)

        res = self.sum_li[min(self.n-1, b-1)]
        if a > 0:
            res -= self.sum_li[a-1]
        return res Interval i += 1 1 i def s_in():
    return input() def s_in():
    return input() s_in
def s_in():
    return input()

def n_in():
    return int(input())

def l_in():
    return list(map(int, input().split()))

class Interval():
    def __init__(self, li):
        self.li = li
        self.n = len(li)
        self.sum_li = [li[0]]
        for i in range(1, self.n):
            self.sum_li.append(self.sum_li[i-1] + li[i])

    def sum(self, a, b=None):
        if b is None:
            return self.sum(0, a)

        res = self.sum_li[min(self.n-1, b-1)]
        if a > 0:
            res -= self.sum_li[a-1]
        return res

N = s_in()[::-1]
n = len(N)

dp1 = [0 for _ in range(n)]
dp1[0] = int(N[0])
# dp1[i] は i桁目を同じ値だけ使ったとしたときの最小値
dp2 = [0 for _ in range(n)]
dp2[0] = 10 - int(N[0])
# dp2[i] は i桁目を繰り上げたとしたときの最小値

for i, s in enumerate(N[1:]):
    i += 1

    m = int(s)
    dp1[i] = min(dp1[i-1] + m,      dp2[i-1] + (m+1))
    dp2[i] = min(dp1[i-1] + (10-m), dp2[i-1] + (10-m-1))

# print(dp1)
# print( dp2)
print(min(dp1[n-1], dp2[n-1]+1))
