import sys, re
from collections import deque, defaultdict, Counter
from math import log10, ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
import numpy as np

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = input()

dp = (0, 1)  # 左:ちょうど  右:1だけ多い
for i in N:
	i = int(i)
	dp = (min(dp[0] + i, dp[1] + 10 - i), min(dp[0] + i + 1, dp[1] + 9 - i))
print(dp[0]) import sys, re sys re from collections import deque, defaultdict, Counter from math import log10, ceil, sqrt, hypot, factorial, pi, sin, cos, radians from itertools import accumulate, permutations, combinations, product from operator import itemgetter, mul from copy import deepcopy from string import ascii_lowercase, ascii_uppercase, digits from bisect import bisect, bisect_left from fractions import gcd from heapq import heappush, heappop from functools import reduce import numpy as np numpy def input(): return sys.stdin.readline().strip() input def INT(): return int(input()) INT def MAP(): return map(int, input().split()) MAP def LIST(): return list(map(int, input().split())) LIST sys.setrecursionlimit(10 ** 9) sys.setrecursionlimit sys setrecursionlimit 10 ** 9 10 9 INF = float('inf') INF float('inf') float 'inf' mod = 10 ** 9 + 7 mod 10 ** 9 + 7 10 ** 9 10 9 7 N = input() N input() input dp = (0, 1) dp (0, 1) 0 1 for i in N:
	i = int(i)
	dp = (min(dp[0] + i, dp[1] + 10 - i), min(dp[0] + i + 1, dp[1] + 9 - i)) i N i = int(i) i int(i) int i dp = (min(dp[0] + i, dp[1] + 10 - i), min(dp[0] + i + 1, dp[1] + 9 - i)) dp (min(dp[0] + i, dp[1] + 10 - i), min(dp[0] + i + 1, dp[1] + 9 - i)) min(dp[0] + i, dp[1] + 10 - i) min dp[0] + i dp[0] dp 0 i dp[1] + 10 - i dp[1] + 10 dp[1] dp 1 10 i min(dp[0] + i + 1, dp[1] + 9 - i) min dp[0] + i + 1 dp[0] + i dp[0] dp 0 i 1 dp[1] + 9 - i dp[1] + 9 dp[1] dp 1 9 i print(dp[0]) print dp[0] dp 0 def MAP(): return map(int, input().split()) def MAP(): return map(int, input().split()) MAP INF = float('inf') float('inf') INF def LIST(): return list(map(int, input().split())) def LIST(): return list(map(int, input().split())) LIST N = input() input() N dp = (min(dp[0] + i, dp[1] + 10 - i), min(dp[0] + i + 1, dp[1] + 9 - i)) (min(dp[0] + i, dp[1] + 10 - i), min(dp[0] + i + 1, dp[1] + 9 - i)) dp mod = 10 ** 9 + 7 10 ** 9 + 7 mod dp = (0, 1) (0, 1) dp def INT(): return int(input()) def INT(): return int(input()) INT i = int(i) int(i) i def input(): return sys.stdin.readline().strip() def input(): return sys.stdin.readline().strip() input
import sys, re
from collections import deque, defaultdict, Counter
from math import log10, ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
import numpy as np

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = input()

dp = (0, 1)  # 左:ちょうど  右:1だけ多い
for i in N:
	i = int(i)
	dp = (min(dp[0] + i, dp[1] + 10 - i), min(dp[0] + i + 1, dp[1] + 9 - i))
print(dp[0])
