import sys
from heapq import heappop, heappush


def main():
    risks = [[int(c) for c in line.strip()] for line in sys.stdin]

    width = len(risks[0])
    height = len(risks)

    for repeat_count in [1, 5]:
        repeated_width = repeat_count * width
        repeated_height = repeat_count * height

        destination = repeated_width - 1, repeated_height - 1
        min_total_risks = {}
        queue = [(0, (0, 0))]

        while queue:
            total_risk, a = heappop(queue)

            if a in min_total_risks:
                continue

            min_total_risks[a] = total_risk

            if a == destination:
                break

            ax, ay = a

            for dx, dy in [(0, -1), (0, 1), (1, 0), (-1, 0)]:
                bx = ax + dx
                by = ay + dy

                if 0 <= bx < repeated_width and 0 <= by < repeated_height:
                    b = bx, by

                    if b in min_total_risks:
                        continue

                    qx, rx = divmod(bx, width)
                    qy, ry = divmod(by, height)

                    risk = (risks[ry][rx] + qx + qy - 1) % 9 + 1
                    heappush(queue, (total_risk + risk, b))

        print(min_total_risks[destination])


if __name__ == "__main__":
    main()
