Greg the Dwarf has been really busy recently with excavations by the Neverland Mountain. However for the well-known reasons (as you probably remember he is a very unusual dwarf and he cannot stand sunlight) Greg can only excavate at night. And in the morning he should be in his crypt before the first sun ray strikes. That's why he wants to find the shortest route from the excavation point to his crypt. Greg has recollected how the Codeforces participants successfully solved the problem of transporting his coffin to a crypt. So, in some miraculous way Greg appeared in your bedroom and asks you to help him in a highly persuasive manner. As usual, you didn't feel like turning him down.

After some thought, you formalized the task as follows: as the Neverland mountain has a regular shape and ends with a rather sharp peak, it can be represented as a cone whose base radius equals r and whose height equals h. The graveyard where Greg is busy excavating and his crypt can be represented by two points on the cone's surface. All you've got to do is find the distance between points on the cone's surface. 

The task is complicated by the fact that the mountain's base on the ground level and even everything below the mountain has been dug through by gnome (one may wonder whether they've been looking for the same stuff as Greg...). So, one can consider the shortest way to pass not only along the side surface, but also along the cone's base (and in a specific case both points can lie on the cone's base — see the first sample test)

Greg will be satisfied with the problem solution represented as the length of the shortest path between two points — he can find his way pretty well on his own. He gave you two hours to solve the problem and the time is ticking!

Input

The first input line contains space-separated integers r and h (1 ≤ r, h ≤ 1000) — the base radius and the cone height correspondingly. The second and third lines contain coordinates of two points on the cone surface, groups of three space-separated real numbers. The coordinates of the points are given in the systems of coordinates where the origin of coordinates is located in the centre of the cone's base and its rotation axis matches the OZ axis. In this coordinate system the vertex of the cone is located at the point (0, 0, h), the base of the cone is a circle whose center is at the point (0, 0, 0), lying on the XOY plane, and all points on the cone surface have a non-negative coordinate z. It is guaranteed that the distances from the points to the cone surface do not exceed 10 - 12. All real numbers in the input have no more than 16 digits after decimal point.

Output

Print the length of the shortest path between the points given in the input, with absolute or relative error not exceeding 10 - 6.

Examples

Input

2 2
1.0 0.0 0.0
-1.0 0.0 0.0


Output

2.000000000

Input

2 2
1.0 0.0 0.0
1.0 0.0 1.0


Output

2.414213562

Input

2 2
1.0 0.0 1.0
-1.0 0.0 1.0


Output

2.534324263

Input

2 2
1.0 0.0 0.0
0.0 1.0 1.0


Output

3.254470198

Difficulty: VERY_HARD
Tags: Geometry

Starter code:

Solutions:
import math
mas = list(map(int, input().split()))
r = mas[0]
h = mas[1]
l = (r ** 2 + h ** 2) ** 0.5
mas = list(map(float, input().split()))
x1 = mas[0]
y1 = mas[1]
z1 = mas[2]
mas = list(map(float, input().split()))
x2 = mas[0]
y2 = mas[1]
z2 = mas[2]
s = 0
if z1 == 0 and z2 == 0:
	s = ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5
elif z1 != 0 and z2 != 0:
	if x1 ** 2 + y1 ** 2 == 0 or x2 ** 2 + y2 ** 2 == 0:
		alf = 0
	else:
		alf = math.acos((x1 * x2 + y1 * y2) / math.sqrt((x1 ** 2 + y1 ** 2) * (x2 ** 2 + y2 ** 2)))
	l1 = l * (1 - z1 / h)
	l2 = l * (1 - z2 / h)
	xx1 = l1
	yy1 = 0
	xx1_ = l1 * math.cos(2 * math.pi * r / l)
	yy1_ = l1 * math.sin(2 * math.pi * r / l)
	phi = alf * r / l
	xx2 = l2 * math.cos(phi)
	yy2 = l2 * math.sin(phi)
	sbok = min(((xx1 - xx2) ** 2 + (yy1 - yy2) ** 2) ** 0.5, ((xx1_ - xx2) ** 2 + (yy1_ - yy2) ** 2) ** 0.5)
	step1 = 2 * math.pi
	step2 = 2 * math.pi
	start1 = 0
	start2 = 0
	smin = 1000000000000000
	sbok1min = 0
	sbok2min = 0
	sosnmin = 0
	alf1min = 0
	alf2min = 0
	phi1min = 0
	phi2min = 0
	i1min = 0
	i2min = 0
	for j in range(20):
		k = 80
		i1 = 0
		for i1 in range(k + 1):
			alf1 = start1 + i1 / k * step1
			phi1 = alf1 * r / l
			xxx1 = l * math.cos(phi1)
			yyy1 = l * math.sin(phi1)
			xxx1_ = r * math.cos(alf1)
			yyy1_ = r * math.sin(alf1)
			i2 = 0
			for i2 in range(k + 1):
				alf2 = start2 + i2 / k * step2
				phi2 = alf2 * r / l
				xxx2 = l * math.cos(phi2)
				yyy2 = l * math.sin(phi2)
				xxx2_ = r * math.cos(alf2)
				yyy2_ = r * math.sin(alf2)
				sbok1 = min((xx1 - xxx1) ** 2 + (yy1 - yyy1) ** 2, (xx1_ - xxx1) ** 2 + (yy1_ - yyy1) ** 2) ** 0.5
				sbok2 = ((xx2 - xxx2) ** 2 + (yy2 - yyy2) ** 2) ** 0.5
				sosn = ((xxx1_ - xxx2_) ** 2 + (yyy1_ - yyy2_) ** 2) ** 0.5
				ss = sbok1 + sbok2 + sosn
				if ss < smin:
					smin = ss
					alf1min = alf1
					start1min = alf1min - step1 / k
					i1min = i1
					alf2min = alf2
					start2min = alf2min - step2 / k
					i2min = i2
					sbok1min = sbok1
					sbok2min = sbok2
					sosnmin = sosn
					phi1min = phi1
					phi2min = phi2
		step1 = step1 / k * 2
		start1 = start1min
		if start1 < 0:
			start1 = 0
		step2 = step2 / k * 2
		start2 = start2min
		if start2 < 0:
			start2 = 0
	s = min(sbok, smin)
else:
	if z1 == 0:
		xtemp = x2
		ytemp = y2
		ztemp = z2
		x2 = x1
		y2 = y1
		z2 = z1
		x1 = xtemp
		y1 = ytemp
		z1 = ztemp
	if x1 ** 2 + y1 ** 2 == 0 or x2 ** 2 + y2 ** 2 == 0:
		alf = 0
	else:
		alf = math.acos((x1 * x2 + y1 * y2) / math.sqrt((x1 ** 2 + y1 ** 2) * (x2 ** 2 + y2 ** 2)))
	l1 = l * (1 - z1 / h)
	xx1 = l1
	yy1 = 0
	xx1_ = l1 * math.cos(2 * math.pi * r / l)
	yy1_ = l1 * math.sin(2 * math.pi * r / l)
	step1 = 2 * math.pi
	start1 = 0
	smin = 1000000000000000
	sbok1min = 0
	alf1min = 0
	phi1min = 0
	i1min = 0
	for j in range(30):
		k = 1000
		i1 = 0
		for i1 in range(k + 1):
			alf1 = start1 + i1 / k * step1
			phi1 = alf1 * r / l
			xxx1 = l * math.cos(phi1)
			yyy1 = l * math.sin(phi1)
			xxx1_ = r * math.cos(alf1)
			yyy1_ = r * math.sin(alf1)
			sbok1 = min((xx1 - xxx1) ** 2 + (yy1 - yyy1) ** 2, (xx1_ - xxx1) ** 2 + (yy1_ - yyy1) ** 2) ** 0.5
			rosn = (x2 ** 2 + y2 ** 2) ** 0.5
			xosn = rosn * math.cos(alf)
			yosn = rosn * math.sin(alf)
			sosn = ((xxx1_ - xosn) ** 2 + (yyy1_ - yosn) ** 2) ** 0.5
			ss = sbok1 + sosn
			if ss < smin:
				smin = ss
				alf1min = alf1
				start1min = alf1min - step1 / k
				i1min = i1
				sbok1min = sbok1
				sosnmin = sosn
				phi1min = phi1
		step1 = step1 / k * 2
		start1 = start1min
		if start1 < 0:
			start1 = 0
	s = smin
print(s)