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 <typename T, typename U>
inline void smin(T &a, const U &b) {
  if (a > b) a = b;
}
struct point {
  double x, y, z;
  inline double al() { return atan2(y, x); }
  inline double nor() { return sqrt(nor2()); }
  inline double nor2() { return *this * *this; }
  inline point(double x = 0, double y = 0, double z = 0) : x(x), y(y), z(z) {}
  inline double operator*(point a) { return x * a.x + y * a.y + z * a.z; }
  inline point operator-(point a) { return point(x - a.x, y - a.y, z - a.z); }
  inline point norm() {
    double s = nor();
    if (s < 1e-7) return *this;
    return point(x / s, y / s, z / s);
  }
  inline void input() { scanf("%lf %lf %lf", &x, &y, &z); }
};
double whole = 2 * 3.1415926535897932384626433832795, step = whole / 3;
double r, h, R;
point A, AA, B;
inline double calc_laterals(point a, point b) {
  double al = fabs(a.al() - b.al());
  smin(al, whole - al);
  double dada = (a - point(0, 0, h)).nor2(), da = sqrt(dada);
  double dbdb = (b - point(0, 0, h)).nor2(), db = sqrt(dbdb);
  return sqrt(dada + dbdb - 2 * da * db * cos(r * al / R));
}
inline double solve() {
  double ans = 1e13;
  for (double sst = 0; sst < whole; sst += step) {
    double st = sst, ed = st + step + 0.1;
    while (ed - st > 1e-7) {
      double a = (st + st + ed) / 3, b = (st + ed + ed) / 3;
      point aa = point(r * cos(a), r * sin(a));
      double sa = (AA - aa).nor() + calc_laterals(aa, B);
      point bb = point(r * cos(b), r * sin(b));
      double sb = (AA - bb).nor() + calc_laterals(bb, B);
      if (sa > sb)
        st = a;
      else
        ed = b;
    }
    point aa = point(r * cos(st), r * sin(st));
    smin(ans, (AA - aa).nor() + calc_laterals(aa, B));
  }
  return ans;
}
int main() {
  scanf("%lf %lf", &r, &h);
  R = sqrt(r * r + h * h);
  A.input(), B.input();
  if (B.z < 1e-7) swap(A, B);
  double ans;
  if (A.z < 1e-7) {
    if (B.z < 1e-7)
      ans = (A - B).nor();
    else
      AA = A, ans = solve();
  } else {
    ans = calc_laterals(A, B);
    for (double sst = 0; sst < whole; sst += step) {
      double st = sst, ed = st + step + 0.1;
      while (ed - st > 1e-7) {
        double a = (st + st + ed) / 3, b = (st + ed + ed) / 3;
        AA = point(r * cos(a), r * sin(a));
        double sa = calc_laterals(A, AA) + solve();
        AA = point(r * cos(b), r * sin(b));
        double sb = calc_laterals(A, AA) + solve();
        if (sa > sb)
          st = a;
        else
          ed = b;
      }
      AA = point(r * cos(st), r * sin(st));
      smin(ans, calc_laterals(A, AA) + solve());
    }
  }
  printf("%.7lf", ans);
}