Hyakugoku has just retired from being the resident deity of the South Black Snail Temple in order to pursue her dream of becoming a cartoonist. She spent six months in that temple just playing "Cat's Cradle" so now she wants to try a different game — "Snakes and Ladders". Unfortunately, she already killed all the snakes, so there are only ladders left now. 

The game is played on a $10 \times 10$ board as follows:  At the beginning of the game, the player is at the bottom left square.  The objective of the game is for the player to reach the Goal (the top left square) by following the path and climbing vertical ladders. Once the player reaches the Goal, the game ends.  The path is as follows: if a square is not the end of its row, it leads to the square next to it along the direction of its row; if a square is the end of its row, it leads to the square above it. The direction of a row is determined as follows: the direction of the bottom row is to the right; the direction of any other row is opposite the direction of the row below it. See Notes section for visualization of path.  During each turn, the player rolls a standard six-sided dice. Suppose that the number shown on the dice is $r$. If the Goal is less than $r$ squares away on the path, the player doesn't move (but the turn is performed). Otherwise, the player advances exactly $r$ squares along the path and then stops. If the player stops on a square with the bottom of a ladder, the player chooses whether or not to climb up that ladder. If she chooses not to climb, then she stays in that square for the beginning of the next turn.  Some squares have a ladder in them. Ladders are only placed vertically — each one leads to the same square of some of the upper rows. In order for the player to climb up a ladder, after rolling the dice, she must stop at the square containing the bottom of the ladder. After using the ladder, the player will end up in the square containing the top of the ladder. She cannot leave the ladder in the middle of climbing. And if the square containing the top of the ladder also contains the bottom of another ladder, she is not allowed to use that second ladder.  The numbers on the faces of the dice are 1, 2, 3, 4, 5, and 6, with each number having the same probability of being shown. 

Please note that:     it is possible for ladders to overlap, but the player cannot switch to the other ladder while in the middle of climbing the first one;     it is possible for ladders to go straight to the top row, but not any higher;     it is possible for two ladders to lead to the same tile;     it is possible for a ladder to lead to a tile that also has a ladder, but the player will not be able to use that second ladder if she uses the first one;     the player can only climb up ladders, not climb down. 

Hyakugoku wants to finish the game as soon as possible. Thus, on each turn she chooses whether to climb the ladder or not optimally. Help her to determine the minimum expected number of turns the game will take.


-----Input-----

Input will consist of ten lines. The $i$-th line will contain 10 non-negative integers $h_{i1}, h_{i2}, \dots, h_{i10}$. If $h_{ij}$ is $0$, then the tile at the $i$-th row and $j$-th column has no ladder. Otherwise, the ladder at that tile will have a height of $h_{ij}$, i.e. climbing it will lead to the tile $h_{ij}$ rows directly above. It is guaranteed that $0 \leq h_{ij} < i$. Also, the first number of the first line and the first number of the last line always contain $0$, i.e. the Goal and the starting tile never have ladders.


-----Output-----

Print only one line containing a single floating-point number — the minimum expected number of turns Hyakugoku can take to finish the game. Your answer will be considered correct if its absolute or relative error does not exceed $10^{-6}$.


-----Examples-----
Input
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0

Output
33.0476190476

Input
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 3 0 0 0 4 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 4 0 0 0
0 0 3 0 0 0 0 0 0 0
0 0 4 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 9

Output
20.2591405923

Input
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 6 6 6 6 6 6 0 0 0
1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0

Output
15.9047592939



-----Note-----

A visualization of the path and the board from example 2 is as follows: [Image]

The tile with an 'S' is the starting tile and the tile with an 'E' is the Goal.

For the first example, there are no ladders.

For the second example, the board looks like the one in the right part of the image (the ladders have been colored for clarity).

It is possible for ladders to overlap, as is the case with the red and yellow ladders and green and blue ladders. It is also possible for ladders to go straight to the top, as is the case with the black and blue ladders. However, it is not possible for ladders to go any higher (outside of the board). It is also possible that two ladders lead to the same tile, as is the case with the red and yellow ladders. Also, notice that the red and yellow ladders lead to the tile with the orange ladder. So if the player chooses to climb either of the red and yellow ladders, they will not be able to climb the orange ladder. Finally, notice that the green ladder passes through the starting tile of the blue ladder. The player cannot transfer from the green ladder to the blue ladder while in the middle of climbing the green ladder.

Difficulty: HARD
Tags: Dynamic programming

Shortest paths

Probability
Skill types: Dynamic programming

Starter code:

Solutions:
X = [[int(a) for a in input().split()] for _ in range(10)]
Y = [(i // 10, 9 - i % 10 if i // 10 & 1 else i % 10) for i in range(100)]
Z = [[i * 10 + 9 - j if i & 1 else i * 10 + j for j in range(10)] for i in range(10)]
E = [0] * 100
F = [0] * 100
for i in range(1, 6):
	F[i] = E[i] = (sum(E[:i]) + 6) / i
for i in range(6, 100):
	F[i] = E[i] = sum(F[i - 6:i]) / 6 + 1
	(x, y) = Y[i]
	if X[x][y]:
		F[i] = min(E[i], E[Z[x - X[x][y]][y]])
print(F[99])


t = []

def corresp(i, j):
	return 100 - (i * 10 + (j if not i % 2 else 9 - j))
paths = [[i] for i in range(101)]
for i in range(10):
	z = [int(k) for k in input().split(' ')]
	if i % 2:
		t += z[::-1]
	else:
		t += z
	for j in range(10):
		if z[j]:
			paths[corresp(i, j)] = [corresp(i, j), corresp(i - z[j], j)]
res = [0 for i in range(101)]
for i in range(99, 94, -1):
	ri = max(1, 6 / (100 - i))
	for j in range(i + 1, 101):
		ri += res[j] / min(6, 100 - i)
	res[i] = ri
for i in range(94, -1, -1):
	ri = 1
	for j in range(i + 1, i + 7):
		ri += min([res[k] for k in paths[j]]) / 6
	res[i] = ri
print(res[1])


from math import *
c = 10
av = []
for i in range(c):
	l = [int(s) for s in input().split()]
	if i % 2 == 0:
		l.reverse()
	for j in range(c):
		if l[j] % 2 == 0:
			l[j] = c * l[j]
		else:
			l[j] = c * l[j] + c - 1 - 2 * j
	av = l + av
d = [0] * c ** 2
for i in range(c ** 2 - 2, -1, -1):
	rep = max(0, 6 - c ** 2 + 1 + i)
	t = 0
	for j in range(1, 6 - rep + 1):
		t += min(d[i + j], d[i + j + av[i + j]]) + 1
	d[i] = (rep + t) / (6 - rep)
print(d[0])


3
import array
from fractions import Fraction
import functools
import itertools
import math
import os
import sys

def main():
	H = [read_ints() for _ in range(10)]
	print(solve(H))

def pos_idx(x, y):
	i = y * 10
	if y % 2 == 0:
		i += x
	else:
		i += 9 - x
	return i

def idx_pos(i):
	y = i // 10
	if y % 2 == 0:
		x = i % 10
	else:
		x = 9 - i % 10
	return (x, y)

def solve(H):
	dp = [0] * 100
	for i in range(1, 100):
		e = 0
		for d in range(1, 7):
			j = i - d
			if j < 0:
				rem = 7 - d
				e += rem / 6
				e *= 6 / (6 - rem)
				break
			(x, y) = idx_pos(j)
			if H[y][x] != 0:
				dy = y - H[y][x]
				k = pos_idx(x, dy)
				assert idx_pos(k) == (x, dy)
				e += (min(dp[j], dp[k]) + 1) / 6
			else:
				e += (dp[j] + 1) / 6
		dp[i] = e
	return dp[99]
DEBUG = 'DEBUG' in os.environ

def inp():
	return sys.stdin.readline().rstrip()

def read_int():
	return int(inp())

def read_ints():
	return [int(e) for e in inp().split()]

def dprint(*value, sep=' ', end='\n'):
	if DEBUG:
		print(*value, sep=sep, end=end)

def __starting_point():
	main()
__starting_point()


h = [list(map(int, input().split())) for _ in range(10)]
tp = {}

def celli(x, y):
	if x % 2:
		return (9 - x) * 10 + y
	return (9 - x) * 10 + 9 - y
for i in range(10):
	for j in range(10):
		if h[i][j]:
			tp[celli(i, j)] = celli(i - h[i][j], j)
dp = [0] * 94 + [6] * 5 + [0]
for i in range(93, -1, -1):
	tot = 0
	for j in range(1, 7):
		if i + j in tp:
			tot += min(dp[i + j], dp[tp[i + j]])
		else:
			tot += dp[i + j]
	tot /= 6
	tot += 1
	dp[i] = tot
print(dp[0])


import sys

def read_ints():
	return [int(x) for x in sys.stdin.readline().split()]

def ind(i, j):
	return 10 * i + (j if i % 2 == 0 else 9 - j)

def read_field():
	field = [0] * 100
	for i in range(10):
		for (j, h) in enumerate(read_ints()[:10]):
			if h != 0:
				h = ind(i - h, j)
			else:
				h = ind(i, j)
			field[ind(i, j)] = h
	return field

def solve(field):
	dp = [1.0] * 100
	dp[0] = 0.0
	for _ in range(100):
		for i in range(1, 100):
			sum = 1.0
			for j in range(1, 7):
				if i - j >= 0:
					sum += min(dp[i - j], dp[field[i - j]]) / 6
				else:
					sum += dp[i] / 6
			dp[i] = sum
	return dp[99]

def main():
	field = read_field()
	result = solve(field)
	sys.stdout.write('{:.6f}\n'.format(result))

def __starting_point():
	main()
__starting_point()


links = list(range(100))
for i in range(10):
	l = list(map(int, input().split()))
	for (j, h) in enumerate(l):
		coo = i * 10 + (j if i % 2 == 0 else 9 - j)
		target = (i - h) * 10 + (j if (i - h) % 2 == 0 else 9 - j)
		links[coo] = target
exp = [0]
for i in range(1, 6):
	exp.append((1 + sum(exp) * 1 / 6) * 6 / i)
for i in range(6, 100):
	new = 0
	for j in range(1, 7):
		new += min(exp[i - j], exp[links[i - j]])
	exp.append(1 + new / 6)
print(exp[-1])


def pos(x, y):
	if y & 1:
		return y * w + w - 1 - x
	return y * w + x
CUBE = 6
(h, w) = (10, 10)
n = h * w
grid = []
for y in range(h):
	line = list(map(int, input().split()))
	grid.append(line)
grid.reverse()
to = [0] * n
for y in range(h):
	for x in range(w):
		y1 = y + grid[y][x]
		if y1 != y:
			to[pos(x, y)] = pos(x, y + grid[y][x])
exp = [0] * (n + CUBE)
for i in range(n - 2, -1, -1):
	exp[i] = 1
	for j in range(1, CUBE + 1):
		exp_to = exp[i + j] / CUBE
		if i + j < n and to[i + j]:
			exp_to = min(exp_to, exp[to[i + j]] / CUBE)
		exp[i] += exp_to
	if i + CUBE >= n:
		exp[i] = CUBE * exp[i] / (n - 1 - i)
print(f'{exp[0]:.16f}')


board = []
for i in range(10):
	board.append([int(i) for i in input().split()])

def next_squares(x, y):
	current_dir = 1 if x % 2 else -1
	res = []
	for _ in range(6):
		(nx, ny) = (x, y + current_dir)
		if ny < 0 or ny == 10:
			(nx, ny) = (x - 1, y)
			current_dir *= -1
			if nx == -1:
				break
		(x, y) = (nx, ny)
		res.append([x, y])
	return res
from functools import lru_cache

@lru_cache(None)
def dp(i, j, can_climb):
	if i == j == 0:
		return 0
	expected = []
	for (x, y) in next_squares(i, j):
		expected.append(dp(x, y, True))
	score = sum(expected) / len(expected) + 6 / len(expected)
	if can_climb and board[i][j]:
		return min(score, dp(i - board[i][j], j, False))
	return score
print(dp(9, 0, True))