n = list(map(int, input()[::-1])) + [0]
s = 0
res = 0
for i, ni in enumerate(n[:-1]):
    k = ni + s
    if k < 5 or (k == 5 and int(n[i + 1]) < 5):
        res += k
        s = 0
    else:
        res += 10 - k
        s = 1
res += s
print(res) n = list(map(int, input()[::-1])) + [0] n list(map(int, input()[::-1])) + [0] list(map(int, input()[::-1])) list map(int, input()[::-1]) map int input()[::-1] () input ::-1 -1 [0] 0 s = 0 s 0 res = 0 res 0 for i, ni in enumerate(n[:-1]):
    k = ni + s
    if k < 5 or (k == 5 and int(n[i + 1]) < 5):
        res += k
        s = 0
    else:
        res += 10 - k
        s = 1 i ni enumerate(n[:-1]) enumerate n[:-1] n :-1 -1 k = ni + s k ni + s ni s if k < 5 or (k == 5 and int(n[i + 1]) < 5):
        res += k
        s = 0
    else:
        res += 10 - k
        s = 1 k < 5 or (k == 5 and int(n[i + 1]) < 5) k < 5 k 5 k == 5 and int(n[i + 1]) < 5 k == 5 k 5 int(n[i + 1]) < 5 int(n[i + 1]) int n[i + 1] n i + 1 i 1 5 res += k res k s = 0 s 0 res += 10 - k res 10 - k 10 k s = 1 s 1 res += s res s print(res) print res s = 1 1 s k = ni + s ni + s k res += k k res s = 0 0 s n = list(map(int, input()[::-1])) + [0] list(map(int, input()[::-1])) + [0] n s = 0 0 s res = 0 0 res res += s s res res += 10 - k 10 - k res
n = list(map(int, input()[::-1])) + [0]
s = 0
res = 0
for i, ni in enumerate(n[:-1]):
    k = ni + s
    if k < 5 or (k == 5 and int(n[i + 1]) < 5):
        res += k
        s = 0
    else:
        res += 10 - k
        s = 1
res += s
print(res)
