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;
template <class T>
inline void smin(T &a, T b) {
  if (a > b) a = b;
}
struct point {
  long double x, y, z;
  point(long double x = 0, long double y = 0, long double z = 0)
      : x(x), y(y), z(z) {}
  point operator-(point a) { return point(x - a.x, y - a.y, z - a.z); }
  point operator+(point a) { return point(x + a.x, y + a.y, z + a.z); }
  long double operator*(point a) { return x * a.x + y * a.y + z * a.z; }
  long double nor() { return sqrtl(*this * *this); }
  long double al() { return atan2l(y, x); }
  void input() { cin >> x >> y >> z; }
};
long double r, h, L;
const long double pi = acosl(-1);
void print(long double a) { printf("%.18lf\n", (double)a); }
long double ALPHA;
long double calc_1(point a, long double al) {
  long double ans = 0;
  long double be = fabsl(a.al() - al);
  if (be > 2 * pi) be -= 2 * pi;
  long double aa = (point(0, 0, h) - a).nor();
  return sqrtl(aa * aa + L * L -
               2 * aa * L * cosl(min(be, 2 * pi - be) * r / L));
}
point a, b;
long double calc1(long double al) {
  point pp = point(cosl(al) * r, sinl(al) * r, 0);
  return (pp - a).nor() + calc_1(b, al);
}
long double calc2(long double al, long double be) {
  long double alpha = fabsl(al - be);
  if (alpha > 2 * pi) alpha -= 2 * pi;
  return calc_1(a, al) + calc_1(b, be) +
         2 * sinl(min(2 * pi - alpha, alpha) / 2) * r;
}
long double calc(long double al) {
  long double ans = 1e200;
  for (int i = 0; i < 6; i++) {
    long double st = pi / 3 * i, ed = st + pi / 2, x1, x2, y1, y2;
    for (int j = 0; j < 50; j++) {
      x1 = (st * 2 + ed) / 3, x2 = (st + ed * 2) / 3;
      smin(ans, y2 = calc2(al, x2));
      smin(ans, y1 = calc2(al, x1));
      if (y1 > y2)
        st = x1;
      else
        ed = x2;
    }
    smin(ans, calc2(al, ed));
  }
  return ans;
}
int main() {
  cin >> r >> h;
  a.input(), b.input();
  L = sqrtl(r * r + h * h);
  long double ALPHA = r * 2 * pi / L;
  if (a.z < 1e-12 && b.z < 1e-12) {
    print((a - b).nor());
    return 0;
  }
  if (a.z > 1e-12 && b.z > 1e-12) {
    long double al = a.al(), be = b.al();
    point pp = point(0, 0, h);
    long double ans =
        sqrtl((pp - a) * (pp - a) + (pp - b) * (pp - b) -
              2 * (pp - a).nor() * (pp - b).nor() *
                  cosl(min(fabsl(al - be), 2 * pi - fabsl(al - be)) * r / L));
    for (int i = 0; i < 6; i++) {
      long double st = pi / 3 * i, ed = st + pi / 2, x1, x2;
      for (int j = 0; j < 50; j++) {
        x1 = (st * 2 + ed) / 3, x2 = (st + ed * 2) / 3;
        if (calc(x1) > calc(x2))
          st = x1;
        else
          ed = x2;
      }
      smin(ans, calc(ed));
    }
    print(ans);
    return 0;
  }
  if (a.z > 1e-12) swap(a, b);
  long double ans = 1e200;
  for (int i = 0; i < 6; i++) {
    long double st = pi / 3 * i, ed = st + pi / 2, x1, x2;
    for (int j = 0; j < 50; j++) {
      x1 = (st * 2 + ed) / 3, x2 = (st + ed * 2) / 3;
      if (calc1(x1) > calc1(x2))
        st = x1;
      else
        ed = x2;
    }
    smin(ans, calc1(ed));
  }
  print(ans);
  return 0;
}