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>
T abs(T a) {
  return a < 0 ? -a : a;
}
template <typename T>
T sqr(T a) {
  return a * a;
}
const int INF = (int)1e9;
const long double EPS = 1e-13;
const long double PI = 3.1415926535897932384626433832795;
const int MAGIC = 10;
const int ITER = 50;
struct pt3 {
  long double first, second, Z;
  pt3() { first = second = Z = 0; }
  pt3(const long double& first, const long double& second, const long double& Z)
      : first(first), second(second), Z(Z) {}
};
inline long double dist(const pt3& a, const pt3& b) {
  return sqrtl(sqr(a.first - b.first) + sqr(a.second - b.second) +
               sqr(a.Z - b.Z));
}
inline long double dist2(const pt3& a, const pt3& b) {
  return (sqr(a.first - b.first) + sqr(a.second - b.second) + sqr(a.Z - b.Z));
}
inline long double leng(const pt3& a) {
  return sqrtl(a.first * a.first + a.second * a.second + a.Z * a.Z);
}
inline pt3 operator-(const pt3& a, const pt3& b) {
  return pt3(a.first - b.first, a.second - b.second, a.Z - b.Z);
}
inline pt3 norm(const pt3& a, const long double& nln) {
  long double ln = leng(a);
  return pt3(a.first * (nln / ln), a.second * (nln / ln), a.Z * (nln / ln));
}
inline long double distAng(const long double& a, const long double& b) {
  if (a > b)
    return min(a - b, b + 2 * PI - a);
  else
    return min(b - a, a + 2 * PI - b);
}
long double h, r;
pt3 a, b, ap, bp, t;
long double aa, ba, ard, brd, ad, bd, ar, br, trd;
inline pt3 getPoint(const long double& ang) {
  return pt3(r * cos(ang), r * sin(ang), 0);
}
inline pt3 project(const pt3& a) {
  pt3 v = norm(a - t, sqrtl(sqr(a.Z) + dist2(a, t) - sqr(h - a.Z)));
  return pt3(a.first + v.first, a.second + v.second, a.Z + v.Z);
}
inline long double distCon(const long double& z1, const long double& z2,
                           long double ang) {
  while (ang < 0) ang += 2 * PI;
  while (ang > 2 * PI) ang -= 2 * PI;
  ang *= r / trd;
  pair<long double, long double> a(trd * (h - z1) / h, 0);
  long double r2 = trd * (h - z2) / h;
  pair<long double, long double> b(r2 * cos(ang), r2 * sin(ang));
  return sqrtl(sqr(a.first - b.first) + sqr(a.second - b.second));
}
inline long double distCon(const pt3& a, const pt3& b) {
  long double dif = atan2(a.second, a.first) - atan2(b.second, b.first);
  while (dif < 0) dif += 2 * PI;
  while (dif > 2 * PI) dif -= 2 * PI;
  return min(distCon(a.Z, b.Z, dif), distCon(a.Z, b.Z, (2 * PI - dif)));
}
long double solve2(const pt3& ca) {
  if (abs(b.Z) < EPS) return dist(ca, b);
  int curMagic = (a.Z < EPS) ? (MAGIC * MAGIC) : (MAGIC);
  long double d = 2 * PI / curMagic;
  long double ans = 1e100;
  for (int i = 0; i < int(curMagic); ++i) {
    long double lf = i * d;
    long double rg = i * d + d;
    for (int j = 0; j < int(ITER); ++j) {
      long double d = (rg - lf) / 3;
      long double ml = lf + d;
      long double mr = rg - d;
      pt3 pl = getPoint(ml), pr = getPoint(mr);
      if (dist(ca, pl) +
              min(distCon(0, b.Z, ml - ba), distCon(0, b.Z, ba - ml)) >
          dist(ca, pr) +
              min(distCon(0, b.Z, mr - ba), distCon(0, b.Z, ba - mr))) {
        lf = ml;
      } else {
        rg = mr;
      }
    }
    ans = min(ans, dist(ca, getPoint(lf)) +
                       min(distCon(0, b.Z, lf - ba), distCon(0, b.Z, ba - lf)));
  }
  return ans;
}
long double solve1() {
  if (abs(a.Z) < EPS) return solve2(a);
  long double d = 2 * PI / MAGIC;
  long double ans = 1e100;
  for (int i = 0; i < int(MAGIC); ++i) {
    long double lf = i * d;
    long double rg = i * d + d;
    for (int j = 0; j < int(ITER); ++j) {
      long double d = (rg - lf) / 3;
      long double ml = lf + d;
      long double mr = rg - d;
      pt3 pl = getPoint(ml), pr = getPoint(mr);
      if (min(distCon(0, a.Z, ml - aa), distCon(0, a.Z, aa - ml)) + solve2(pl) >
          min(distCon(0, a.Z, mr - aa), distCon(0, a.Z, aa - mr)) +
              solve2(pr)) {
        lf = ml;
      } else {
        rg = mr;
      }
    }
    ans = min(ans, min(distCon(0, a.Z, lf - aa), distCon(0, a.Z, aa - lf)) +
                       solve2(getPoint(lf)));
  }
  return ans;
}
int main() {
  cout.precision(10);
  cout << fixed;
  cin >> r >> h >> a.first >> a.second >> a.Z >> b.first >> b.second >> b.Z;
  if (b.Z < EPS) swap(a, b);
  t = pt3(0, 0, h);
  trd = sqrtl(r * r + h * h);
  if (a.Z < EPS && b.Z < EPS) {
    cout << dist(a, b) << endl;
    return 0;
  }
  if (a.first < EPS && a.second < EPS && abs(a.Z - h) < EPS) {
    if (b.Z < EPS) {
      cout << trd + r - dist(b, pt3(0, 0, 0)) << endl;
    } else {
      cout << dist(a, b) << endl;
    }
    return 0;
  }
  if (b.first < EPS && b.second < EPS && abs(b.Z - h) < EPS) {
    if (a.Z < EPS) {
      cout << trd + r - dist(a, pt3(0, 0, 0)) << endl;
    } else {
      cout << dist(a, b) << endl;
    }
    return 0;
  }
  if (abs(a.Z) > EPS) {
    ap = project(a);
    ad = dist(a, ap);
    ard = sqrtl(r * r + h * h) / h * (h - a.Z);
    aa = atan2(ap.second, ap.first);
    while (aa < 0) aa += 2 * PI;
    ar = sqrtl(dist2(a, t) - sqr(h - a.Z));
  }
  if (abs(b.Z) > EPS) {
    bp = project(b);
    bd = dist(b, ap);
    brd = sqrtl(r * r + h * h) / h * (h - b.Z);
    ba = atan2(bp.second, bp.first);
    while (ba < 0) ba += 2 * PI;
    br = sqrtl(dist2(b, t) - sqr(h - b.Z));
  }
  long double conDist = 1e100;
  if (abs(a.Z) > EPS && abs(b.Z) > EPS) {
    conDist = distCon(a, b);
  }
  cout << min(conDist, solve1()) << endl;
  cerr << clock() << endl;
  return 0;
}