Problem: 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

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9, inf = 1e9 + 9, pi = acos(-1.0), dtheta = pi / 4.;
double ans, r, h, xa, ya, za, xb, yb, zb, la, ra, mxa, mya, lb, rb, mxb, myb,
    lv, rv, lz, rz;
double dist(double x1, double y1, double z1, double x2, double y2, double z2) {
  return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) +
              (z1 - z2) * (z1 - z2));
}
void norm(double &theta) {
  while (theta < -eps) theta += 2 * pi;
  while (theta > 2 * pi - eps) theta -= 2 * pi;
}
double solve(double x1, double y1, double z1, double x2, double y2, double z2) {
  double l1, l2, l, cj, alpha, dj, theta, res;
  if (fabs(z1) < eps && fabs(z2) < eps) return dist(x1, y1, z1, x2, y2, z2);
  l1 = dist(x1, y1, z1, 0, 0, h);
  l2 = dist(x2, y2, z2, 0, 0, h);
  alpha = atan2(y2, x2) - atan2(y1, x1);
  norm(alpha);
  if (alpha > pi) alpha = 2 * pi - alpha;
  l = alpha * r;
  cj = sqrt(r * r + h * h);
  theta = l / cj;
  double u1, v1, u2, v2;
  u1 = 0;
  v1 = l1;
  u2 = l2 * sin(theta);
  v2 = l2 * cos(theta);
  res = sqrt((u1 - u2) * (u1 - u2) + (v1 - v2) * (v1 - v2));
  theta = 2 * pi - theta;
  u1 = 0;
  v1 = l1;
  u2 = l2 * sin(theta);
  v2 = l2 * cos(theta);
  res = min(res, sqrt((u1 - u2) * (u1 - u2) + (v1 - v2) * (v1 - v2)));
  return res;
}
int main() {
  int i, j, s, p, q, it1, it2;
  scanf("%lf%lf", &r, &h);
  scanf("%lf%lf%lf%lf%lf%lf", &xa, &ya, &za, &xb, &yb, &zb);
  ans = inf;
  if (fabs(za) > eps && fabs(zb) > eps)
    ans = min(ans, solve(xa, ya, za, xb, yb, zb));
  if (fabs(za) < eps && fabs(zb) < eps)
    ans = min(ans, dist(xa, ya, za, xb, yb, zb));
  if (fabs(zb) < eps) {
    swap(xa, xb);
    swap(ya, yb);
    swap(za, zb);
  }
  la = 0;
  ra = 2 * pi;
  for (it1 = 0; it1 < 1000; it1++) {
    mxa = (29. * la + ra) / 30.;
    mya = (la + 29. * ra) / 30.;
    lv = solve(xa, ya, za, r * cos(mxa), r * sin(mxa), 0) +
         solve(xb, yb, zb, r * cos(mxa), r * sin(mxa), 0);
    rv = solve(xa, ya, za, r * cos(mya), r * sin(mya), 0) +
         solve(xb, yb, zb, r * cos(mya), r * sin(mya), 0);
    ans = min(ans, lv);
    ans = min(ans, rv);
    if (lv > rv)
      la = mxa;
    else
      ra = mya;
  }
  la = 0;
  ra = 2 * pi;
  for (it1 = 0; it1 < 600; it1++) {
    mxa = (49. * la + ra) / 50.;
    mya = (la + 49. * ra) / 50.;
    lb = 0;
    rb = 2 * pi;
    for (it2 = 0; it2 < 600; it2++) {
      mxb = (49.0 * lb + rb) / 50.;
      myb = (lb + 49.0 * rb) / 50.;
      lv = solve(xa, ya, za, r * cos(mxa), r * sin(mxa), 0) +
           solve(xb, yb, zb, r * cos(mxb), r * sin(mxb), 0) +
           dist(r * cos(mxa), r * sin(mxa), 0, r * cos(mxb), r * sin(mxb), 0);
      rv = solve(xa, ya, za, r * cos(mxa), r * sin(mxa), 0) +
           solve(xb, yb, zb, r * cos(myb), r * sin(myb), 0) +
           dist(r * cos(mxa), r * sin(mxa), 0, r * cos(myb), r * sin(myb), 0);
      if (lv > rv)
        lb = mxb;
      else
        rb = myb;
    }
    mxb = 0.5 * (lb + rb);
    lz = solve(xa, ya, za, r * cos(mxa), r * sin(mxa), 0) +
         solve(xb, yb, zb, r * cos(mxb), r * sin(mxb), 0) +
         dist(r * cos(mxa), r * sin(mxa), 0, r * cos(mxb), r * sin(mxb), 0);
    lb = 0;
    rb = 2 * pi;
    for (it2 = 0; it2 < 600; it2++) {
      mxb = (49. * lb + rb) / 50.;
      myb = (lb + 49. * rb) / 50.;
      lv = solve(xa, ya, za, r * cos(mya), r * sin(mya), 0) +
           solve(xb, yb, zb, r * cos(mxb), r * sin(mxb), 0) +
           dist(r * cos(mya), r * sin(mya), 0, r * cos(mxb), r * sin(mxb), 0);
      rv = solve(xa, ya, za, r * cos(mya), r * sin(mya), 0) +
           solve(xb, yb, zb, r * cos(myb), r * sin(myb), 0) +
           dist(r * cos(mya), r * sin(mya), 0, r * cos(myb), r * sin(myb), 0);
      ans = min(ans, lv);
      ans = min(ans, rv);
      if (lv > rv)
        lb = mxb;
      else
        rb = myb;
    }
    mxb = 0.5 * (lb + rb);
    rz = solve(xa, ya, za, r * cos(mya), r * sin(mya), 0) +
         solve(xb, yb, zb, r * cos(mxb), r * sin(mxb), 0) +
         dist(r * cos(mya), r * sin(mya), 0, r * cos(mxb), r * sin(mxb), 0);
    if (lz > rz)
      la = mxa;
    else
      ra = mya;
  }
  printf("%.20f\n", ans);
  return 0;
}