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;
double error = 1e-9;
double r, h, lado, perimetro;
struct point {
  double x, y, z;
  point() {}
  point(double inx, double iny, double inz) {
    x = inx;
    y = iny;
    z = inz;
  }
};
point operator-(point p1, point p2) {
  p1.x -= p2.x;
  p1.y -= p2.y;
  p1.z -= p2.z;
  return p1;
}
point rotar(point p, double a) {
  point nextp = p;
  nextp.x = p.x * cos(a) - p.y * sin(a);
  nextp.y = p.x * sin(a) + p.y * cos(a);
  return nextp;
}
double cuad(double x) { return x * x; }
double abs(point p) { return sqrt(cuad(p.x) + cuad(p.y) + cuad(p.z)); }
point p1, pp1;
point p2, pp2;
point obtenpp(double a) {
  double arg = a * r / lado;
  return point(lado * cos(arg), lado * sin(arg), 0);
}
double distap1(double a) { return abs(pp1 - obtenpp(a)); }
double distap2(double a) { return abs(pp2 - obtenpp(a)); }
int divisor = 500;
double buscar1(double a1, double a2) {
  if (abs(a1 - a2) < error)
    return distap1(a1) + abs(p2 - point(r * cos(a1), r * sin(a1), 0));
  int imin = 0;
  double solmin = distap1(a1) + abs(p2 - point(r * cos(a1), r * sin(a1), 0));
  for (int i = 0; i <= divisor; i++) {
    double a = (a1 * (divisor - i) + a2 * i) / divisor;
    double solact = distap1(a) + abs(p2 - point(r * cos(a), r * sin(a), 0));
    if (solact < solmin) {
      imin = i;
      solmin = solact;
    }
  }
  int i1 = max(0, imin - 1);
  int i2 = min(divisor, imin + 1);
  return buscar1((a1 * (divisor - i1) + a2 * i1) / divisor,
                 (a1 * (divisor - i2) + a2 * i2) / divisor);
}
double buscar2(double a1inf, double a1sup, double a2inf, double a2sup) {
  int imin = 0, jmin = 0;
  double solmin = distap1(a1inf) + distap2(a2inf) +
                  abs(point(r * cos(a1inf), r * sin(a1inf), 0) -
                      point(r * cos(a2inf), r * sin(a2inf), 0));
  if (abs(a1inf - a1sup) < error and abs(a2inf - a2sup) < error) return solmin;
  for (int i = 0; i <= divisor; i++) {
    for (int j = 0; j <= divisor; j++) {
      double aa1 = (a1inf * (divisor - i) + a1sup * i) / divisor;
      double aa2 = (a2inf * (divisor - j) + a2sup * j) / divisor;
      if (aa1 <= aa2) {
        double solact = distap1(aa1) + distap2(aa2) +
                        abs(point(r * cos(aa1), r * sin(aa1), 0) -
                            point(r * cos(aa2), r * sin(aa2), 0));
        if (solact < solmin) {
          imin = i;
          jmin = j;
          solmin = solact;
        }
      }
    }
  }
  int i1 = max(0, imin - 1);
  int i2 = min(divisor, imin + 1);
  int j1 = max(0, jmin - 1);
  int j2 = min(divisor, jmin + 1);
  return buscar2((a1inf * (divisor - i1) + a1sup * i1) / divisor,
                 (a1inf * (divisor - i2) + a1sup * i2) / divisor,
                 (a2inf * (divisor - j1) + a2sup * j1) / divisor,
                 (a2inf * (divisor - j2) + a2sup * j2) / divisor);
}
int main() {
  cout.setf(ios::fixed);
  cout.precision(10);
  cin >> r >> h >> p1.x >> p1.y >> p1.z >> p2.x >> p2.y >> p2.z;
  lado = sqrt(cuad(r) + cuad(h));
  perimetro = 2 * M_PI * r;
  if (p1.z < error and p2.z < error) {
    cout << abs(p1 - p2) << endl;
    exit(0);
  }
  if (p1.z < p2.z) swap(p1, p2);
  if (p1.z > h - error) {
    if (p2.z < error)
      cout << r - sqrt(cuad(p2.x) + cuad(p2.y)) + abs(p1 - point(r, 0, 0))
           << endl;
    else
      cout << abs(p1 - p2) << endl;
    exit(0);
  }
  double a = atan2(p1.y, p1.x);
  p1 = rotar(p1, -a);
  p2 = rotar(p2, -a);
  if (p2.y < 0) p2.y = -p2.y;
  double a1 = 0;
  if (abs(p2 - point(0, 0, 0)) < error) {
    cout << r + abs(p1 - point(r, 0, 0)) << endl;
    exit(0);
  }
  double a2 = atan2(p2.y, p2.x);
  if (a2 < 0) a2 = -a2;
  pp1 = point(abs(point(0, 0, h) - p1), 0, 0);
  double lenp2 = abs(point(0, 0, h) - p2);
  double aa2 = a2 * r / lado;
  pp2 = point(lenp2 * cos(aa2), lenp2 * sin(aa2), 0);
  if (pp2.y < 0) pp2.y = -pp2.y;
  double sol;
  if (p2.z < error) {
    sol = buscar1(a1, a2);
  } else {
    sol = min(abs(pp1 - pp2), buscar2(a1, a2, a1, a2));
  }
  cout << sol << endl;
}