Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
N = str(N)
N = tuple(map(int, N))

ans = 0
flag = False
flag5 = False
i = len(N)-1


for x in reversed(N):
    if flag or (flag5 and x >= 5):
        x += 1
    flag5 = False
    flag = False
    if x > 5:
        ans += (10 - x)
        flag = True
        if i == 0:
            ans += 1
    else:

        ans += x
    if x == 5:
        flag5 = True
    i -= 1

print(ans)


Fixed submission:
N = tuple(input())
N = tuple(map(int, N))

ans = 0
flag = False
flag5 = False
i = len(N)-1


for x in reversed(N):
    if flag or (flag5 and x >= 5):
        x += 1
    flag5 = False
    flag = False
    if x > 5:
        ans += (10 - x)
        flag = True
        if i == 0:
            ans += 1
    else:

        ans += x
    if x == 5:
        flag5 = True
    i -= 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
N = list(str(N))
N = tuple((map(int, N)))

ans = 0
flag = False
flag5 = False
for i in range(len(N)-1, -1, -1):
    x = N[i]
    if flag or (flag5 and x >= 5):
        x += 1
    flag5 = False
    flag = False
    if x > 5:
        ans += (10 - x)
        flag = True
        if i == 0:
            ans += 1
    else:
        ans += x
    if x == 5:
        flag5 = True
print(ans)

Fixed submission:
N = tuple(input())
N = tuple(map(int, N))

ans = 0
flag = False
flag5 = False
i = len(N)-1


for x in reversed(N):
    if flag or (flag5 and x >= 5):
        x += 1
    flag5 = False
    flag = False
    if x > 5:
        ans += (10 - x)
        flag = True
        if i == 0:
            ans += 1
    else:

        ans += x
    if x == 5:
        flag5 = True
    i -= 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
N = list(str(N))
N = list(map(int, N))

ans = 0
for i in range(len(N)-1, -1, -1):

    if i < len(N)-1 and N[i+1] == 5 and N[i] >= 5:
        N[i] += 1
    x = N[i]
    if x > 5:
        ans += (10 - x)
        if i > 0:
            N[i-1] += 1
        else:
            ans += 1
    else:
        ans += x
print(ans)


Fixed submission:
N = tuple(input())
N = tuple(map(int, N))

ans = 0
flag = False
flag5 = False
i = len(N)-1


for x in reversed(N):
    if flag or (flag5 and x >= 5):
        x += 1
    flag5 = False
    flag = False
    if x > 5:
        ans += (10 - x)
        flag = True
        if i == 0:
            ans += 1
    else:

        ans += x
    if x == 5:
        flag5 = True
    i -= 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
N = list(str(N))
N = list(map(int, N))

ans = 0
for i in range(len(N)-1, -1, -1):

    if i < len(N)-1 and N[i+1] == 5 and N[i] >= 5:
        N[i] += 1
    x = N[i]
    if x > 5:
        ans += (10 - x)
        if i > 0:
            N[i-1] += 1
        else:
            ans += 1
    else:
        ans += x
print(ans)

Fixed submission:
N = tuple(input())
N = tuple(map(int, N))

ans = 0
flag = False
flag5 = False
i = len(N)-1


for x in reversed(N):
    if flag or (flag5 and x >= 5):
        x += 1
    flag5 = False
    flag = False
    if x > 5:
        ans += (10 - x)
        flag = True
        if i == 0:
            ans += 1
    else:

        ans += x
    if x == 5:
        flag5 = True
    i -= 1

print(ans)