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 int N = 200005;
const int inf = 1e9;
const long long Inf = 1e10;
const int P = 1000000007;
const long double pi = acos(-1);
long double r, h, Sqr, l;
long double X1, X2, Y1, Y2, Z1, Z2;
void read(long double &x) {
  double z;
  scanf("%lf", &z);
  x = z;
}
long double sqr(long double x) { return x * x; }
long double Fbb(long double A, long double B, long double D, long double E) {
  return sqrt(sqr(A - D) + sqr(B - E));
}
long double Faa(long double A, long double B, long double C, long double D,
                long double E, long double F) {
  if (fabs(C - h) < 1e-6) return sqrt(D * D + E * E + sqr(F - h));
  if (fabs(F - h) < 1e-6) return sqrt(A * A + B * B + sqr(C - h));
  long double X3 = A * h / (h - C), Y3 = B * h / (h - C), X4 = D * h / (h - F),
              Y4 = E * h / (h - F);
  long double u = Sqr * 2 - sqr(X3 - X4) - sqr(Y3 - Y4);
  u /= 2 * Sqr;
  long double the = acos(u);
  long double phi = the * r / l;
  long double D1 = sqr(A) + sqr(B) + sqr(C - h),
              D2 = sqr(D) + sqr(E) + sqr(F - h);
  return sqrt(D1 + D2 - 2 * sqrt(D1) * sqrt(D2) * cos(phi));
}
long double Fab(long double X, long double Y, long double Z, long double E,
                long double F, long double the) {
  long double X3 = r * cos(the), Y3 = r * sin(the);
  return Faa(X, Y, Z, X3, Y3, 0) + Fbb(E, F, X3, Y3);
}
long double Al = 0, Ar = 0;
long double Fab2(long double the) {
  long double X3 = cos(the) * r, Y3 = sin(the) * r;
  long double l = Al, r = Ar, T1, T2;
  for (int i = (int)(1); i <= (int)(400); i++) {
    long double md1 = (9 * l + r) / 10., md2 = (l + 9 * r) / 10.;
    T1 = Fab(X2, Y2, Z2, X3, Y3, md1);
    T2 = Fab(X2, Y2, Z2, X3, Y3, md2);
    if (T1 > T2)
      l = md1;
    else
      r = md2;
  }
  return T1 + Faa(X1, Y1, Z1, X3, Y3, 0);
}
int main() {
  read(r);
  read(h);
  Sqr = r * r;
  l = sqrt(r * r + h * h);
  read(X1);
  read(Y1);
  read(Z1);
  read(X2);
  read(Y2);
  read(Z2);
  if (Z1 == 0 && Z2 == 0) {
    printf("%.16lf\n", double(Fbb(X1, Y1, X2, Y2)));
    return 0;
  }
  if (Z1 != 0 && Z2 != 0) {
    Al = atan2(Y1, X1);
    Ar = atan2(Y2, X2);
    if (fabs(Ar - Al) > pi) {
      X1 = -X1, Y1 = -Y1, X2 = -X2, Y2 = -Y2;
      Al = atan2(Y1, X1);
      Ar = atan2(Y2, X2);
    }
    if (Al > Ar) swap(Al, Ar);
    long double ans = Faa(X1, Y1, Z1, X2, Y2, Z2);
    long double l = Al, r = Ar, T1, T2;
    for (int i = (int)(1); i <= (int)(300); i++) {
      long double md1 = (9 * l + r) / 10., md2 = (l + 9 * r) / 10.;
      T1 = Fab2(md1);
      T2 = Fab2(md2);
      if (T1 > T2)
        l = md1;
      else
        r = md2;
    }
    ans = min(ans, T1);
    printf("%.16lf\n", double(ans));
    return 0;
  }
  if (Z1 == 0) {
    swap(X1, X2);
    swap(Y1, Y2);
    swap(Z1, Z2);
  }
  long double l = 0, r = 2 * pi, T1, T2;
  for (int i = (int)(1); i <= (int)(500); i++) {
    long double md1 = (29 * l + r) / 30., md2 = (l + 29 * r) / 30.;
    if ((T1 = Fab(X1, Y1, Z1, X2, Y2, md1)) >
        (T2 = Fab(X1, Y1, Z1, X2, Y2, md2)))
      l = md1;
    else
      r = md2;
  }
  printf("%.16lf\n", double(T1));
  return 0;
}