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;
using DO = long double;
using pdd = pair<DO, DO>;
pdd operator+(const pdd &a, const pdd &b) {
  return {a.first + b.first, a.second + b.second};
}
pdd operator-(const pdd &a, const pdd &b) {
  return {a.first - b.first, a.second - b.second};
}
pdd operator*(const pdd &a, const DO &b) { return {a.first * b, a.second * b}; }
pdd operator/(const pdd &a, const DO &b) { return {a.first / b, a.second / b}; }
DO operator*(const pdd &a, const pdd &b) {
  return a.first * b.first + a.second * b.second;
}
DO operator^(const pdd &a, const pdd &b) {
  return a.first * b.second - a.second * b.first;
}
DO len(const pdd &a) { return sqrtl(a * a); }
pdd rot(const pdd &a, const DO &alp) {
  return {a.first * cosl(alp) - a.second * sinl(alp),
          a.first * sinl(alp) + a.second * cosl(alp)};
}
DO alp(const pdd &a) { return atan2l(a.second, a.first); }
const DO pi = acosl(-1);
const DO eps = 1e-10;
int sgn(const DO &a) { return a < -eps ? -1 : a > eps; }
DO h, r, R;
pdd get(DO ax, DO ay) {
  pdd p = {ax, ay};
  DO len = ::len(p);
  if (sgn(len) == 0) {
    return {r, 0};
  } else {
    return p * (r / len);
  }
}
DO f(DO ax, DO ay, DO az, DO bx, DO by, DO bz) {
  pdd p, q;
  p = get(ax, ay);
  q = get(bx, by);
  DO ap = alp(p);
  DO aq = alp(q);
  DO al = aq - ap;
  while (al < -eps) al += pi * 2;
  while (al > pi * 2 + eps) al -= pi * 2;
  if (al > pi + eps) {
    al = pi * 2 - al;
  }
  al = al * (r / R);
  DO a = sqrtl(((ax) * (ax)) + ((ay) * (ay)) + ((h - az) * (h - az)));
  DO b = sqrtl(((bx) * (bx)) + ((by) * (by)) + ((h - bz) * (h - bz)));
  DO c = ((a) * (a)) + ((b) * (b)) - 2 * a * b * cosl(al);
  c = max((DO)0, c);
  return sqrtl(c);
}
DO f1(DO ax, DO ay, DO bx, DO by, DO bz, DO ag = 0) {
  pdd p = get(ax, ay);
  pdd p1, p2;
  int step = 100;
  DO st = 0, ed = pi * 2, md1, md2, v1, v2;
  while (step--) {
    md1 = (st + st + ed) / 3;
    md2 = (st + ed + ed) / 3;
    p1 = rot(p, md1 + ag);
    p2 = rot(p, md2 + ag);
    v1 = len(pdd(ax, ay) - p1) + f(p1.first, p1.second, 0, bx, by, bz);
    v2 = len(pdd(ax, ay) - p2) + f(p2.first, p2.second, 0, bx, by, bz);
    if (v1 < v2) {
      ed = md2;
    } else {
      st = md1;
    }
  }
  p1 = rot(p, st + ag);
  v1 = len(pdd(ax, ay) - p1) + f(p1.first, p1.second, 0, bx, by, bz);
  return v1;
}
DO f2(DO ax, DO ay, DO bx, DO by, DO bz) {
  DO ans = f1(ax, ay, bx, by, bz);
  ans = min(ans, f1(ax, ay, bx, by, bz, pi * 2 / 3));
  ans = min(ans, f1(ax, ay, bx, by, bz, pi * 4 / 3));
  return ans;
}
DO f3(DO ax, DO ay, DO az, DO bx, DO by, DO bz, DO ag = 0) {
  DO ans = f(ax, ay, az, bx, by, bz);
  pdd p = get(ax, ay);
  int step = 100;
  DO st = 0, ed = pi * 2, md1, md2, v1, v2;
  pdd p1, p2;
  while (step--) {
    md1 = (st + st + ed) / 3;
    md2 = (st + ed + ed) / 3;
    p1 = rot(p, md1 + ag);
    p2 = rot(p, md2 + ag);
    v1 = f(p1.first, p1.second, 0, ax, ay, az) +
         f2(p1.first, p1.second, bx, by, bz);
    v2 = f(p2.first, p2.second, 0, ax, ay, az) +
         f2(p2.first, p2.second, bx, by, bz);
    if (v1 < v2) {
      ed = md2;
    } else {
      st = md1;
    }
  }
  p1 = rot(p, st + ag);
  v1 = f(p1.first, p1.second, 0, ax, ay, az) +
       f2(p1.first, p1.second, bx, by, bz);
  ans = min(ans, v1);
  return ans;
}
DO f4(DO ax, DO ay, DO az, DO bx, DO by, DO bz) {
  DO ans = f3(ax, ay, az, bx, by, bz);
  ans = min(ans, f3(ax, ay, az, bx, by, bz, pi * 2 / 3));
  ans = min(ans, f3(ax, ay, az, bx, by, bz, pi * 4 / 3));
  return ans;
}
int main() {
  cout << fixed << setprecision(10);
  cin >> r >> h;
  R = sqrtl(((r) * (r)) + ((h) * (h)));
  DO ax, ay, az, bx, by, bz;
  cin >> ax >> ay >> az >> bx >> by >> bz;
  if (sgn(az) == 0 && sgn(bz) == 0) {
    cout << len(pdd(ax - bx, ay - by)) << endl;
    return 0;
  }
  if (sgn(az) == 0) {
    cout << f2(ax, ay, bx, by, bz) << endl;
    return 0;
  }
  if (sgn(bz) == 0) {
    cout << f2(bx, by, ax, ay, az) << endl;
    return 0;
  }
  DO ans = f4(ax, ay, az, bx, by, bz);
  cout << ans << endl;
  return 0;
}