from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
import pprint
sys.setrecursionlimit(10 ** 9)


INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n = S()
dp = [[0] * 2 for _ in range(len(n))]
dp[0] = [int(n[-1]), 11 - int(n[-1])]
for i in range(1, len(n)):
    s = int(n[-i-1])
    dp[i][0] = s + min(dp[i - 1][0], dp[i - 1][1])
    dp[i][1] = min(11 - s + dp[i - 1][0], 9 - s + dp[i - 1][1])


print(min(dp[-1])) from collections import defaultdict, deque, Counter from heapq import heappush, heappop, heapify import math math import bisect bisect import random random from itertools import permutations, accumulate, combinations, product import sys sys import string string from bisect import bisect_left, bisect_right from math import factorial, ceil, floor from operator import mul from functools import reduce import pprint pprint sys.setrecursionlimit(10 ** 9) sys.setrecursionlimit sys setrecursionlimit 10 ** 9 10 9 INF = 10 ** 13 INF 10 ** 13 10 13 def LI(): return list(map(int, sys.stdin.buffer.readline().split())) LI def I(): return int(sys.stdin.buffer.readline()) I def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split() LS def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8') S I() for i in range(n) for i in range(n) i range(n) range n for i in range(n) I() I def IR(n): return [I() for i in range(n)] IR n n LI() for i in range(n) for i in range(n) i range(n) range n for i in range(n) LI() LI def LIR(n): return [LI() for i in range(n)] LIR n n S() for i in range(n) for i in range(n) i range(n) range n for i in range(n) S() S def SR(n): return [S() for i in range(n)] SR n n LS() for i in range(n) for i in range(n) i range(n) range n for i in range(n) LS() LS def LSR(n): return [LS() for i in range(n)] LSR n n list(S()) for i in range(n) for i in range(n) i range(n) range n for i in range(n) list(S()) list S() S def SRL(n): return [list(S()) for i in range(n)] SRL n n [int(j) for j in list(S())] for i in range(n) for i in range(n) i range(n) range n for i in range(n) [int(j) for j in list(S())] def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)] MSRL n n mod = 1000000007 mod 1000000007 n = S() n S() S [0] * 2 for _ in range(len(n)) for _ in range(len(n)) _ range(len(n)) range len(n) len n for _ in range(len(n)) [0] * 2 [0] 0 2 dp = [[0] * 2 for _ in range(len(n))] dp [[0] * 2 for _ in range(len(n))] dp[0] = [int(n[-1]), 11 - int(n[-1])] dp[0] dp 0 [int(n[-1]), 11 - int(n[-1])] int(n[-1]) int n[-1] n -1 11 - int(n[-1]) 11 int(n[-1]) int n[-1] n -1 for i in range(1, len(n)):
    s = int(n[-i-1])
    dp[i][0] = s + min(dp[i - 1][0], dp[i - 1][1])
    dp[i][1] = min(11 - s + dp[i - 1][0], 9 - s + dp[i - 1][1]) i range(1, len(n)) range 1 len(n) len n s = int(n[-i-1]) s int(n[-i-1]) int n[-i-1] n -i-1 -i i 1 dp[i][0] = s + min(dp[i - 1][0], dp[i - 1][1]) dp[i][0] [i] dp i 0 s + min(dp[i - 1][0], dp[i - 1][1]) s min(dp[i - 1][0], dp[i - 1][1]) min dp[i - 1][0] [i - 1] dp i - 1 i 1 0 dp[i - 1][1] [i - 1] dp i - 1 i 1 1 dp[i][1] = min(11 - s + dp[i - 1][0], 9 - s + dp[i - 1][1]) dp[i][1] [i] dp i 1 min(11 - s + dp[i - 1][0], 9 - s + dp[i - 1][1]) min 11 - s + dp[i - 1][0] 11 - s 11 s dp[i - 1][0] [i - 1] dp i - 1 i 1 0 9 - s + dp[i - 1][1] 9 - s 9 s dp[i - 1][1] [i - 1] dp i - 1 i 1 1 print(min(dp[-1])) print min(dp[-1]) min dp[-1] dp -1 def SRL(n): return [list(S()) for i in range(n)] def SRL(n): return [list(S()) for i in range(n)] SRL def IR(n): return [I() for i in range(n)] def IR(n): return [I() for i in range(n)] IR def SR(n): return [S() for i in range(n)] def SR(n): return [S() for i in range(n)] SR dp = [[0] * 2 for _ in range(len(n))] [[0] * 2 for _ in range(len(n))] dp def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split() def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split() LS def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8') def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8') S mod = 1000000007 1000000007 mod def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def LI(): return list(map(int, sys.stdin.buffer.readline().split())) LI def LSR(n): return [LS() for i in range(n)] def LSR(n): return [LS() for i in range(n)] LSR s = int(n[-i-1]) int(n[-i-1]) s def LIR(n): return [LI() for i in range(n)] def LIR(n): return [LI() for i in range(n)] LIR INF = 10 ** 13 10 ** 13 INF def I(): return int(sys.stdin.buffer.readline()) def I(): return int(sys.stdin.buffer.readline()) I n = S() S() n def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)] def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)] MSRL
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
import pprint
sys.setrecursionlimit(10 ** 9)


INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n = S()
dp = [[0] * 2 for _ in range(len(n))]
dp[0] = [int(n[-1]), 11 - int(n[-1])]
for i in range(1, len(n)):
    s = int(n[-i-1])
    dp[i][0] = s + min(dp[i - 1][0], dp[i - 1][1])
    dp[i][1] = min(11 - s + dp[i - 1][0], 9 - s + dp[i - 1][1])


print(min(dp[-1]))
