#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
inline void smin(A &a, const B &b) {
  if (a > b) a = b;
}
struct point {
  double x, y, z;
  inline point(double x = 0, double y = 0, double z = 0) : x(x), y(y), z(z) {}
  inline point operator-(point a) { return point(x - a.x, y - a.y, z - a.z); }
  inline double al() { return atan2(y, x); }
  inline double nor2() { return *this * *this; }
  inline double nor() { return sqrt(*this * *this); }
  inline double operator*(point a) { return x * a.x + y * a.y + z * a.z; }
  inline void input() { scanf("%lf %lf %lf", &x, &y, &z); }
  inline void print() { printf("%lf %lf %lf\n", x, y, z); }
} A, B, A_, B_, F;
double r, h, R;
double all = 6.283185307179586476925286766559;
double _120 = 2.0943951023931954923084289221863;
double _150 = 2.2689280275926284500007979990352;
inline double dist(point a, point b) {
  double al = fabs(a.al() - b.al());
  smin(al, all - al);
  double da = (a - F).nor2(), db = (b - F).nor2();
  return sqrt(da + db - 2 * sqrt(da * db) * cos(r * al / R));
}
inline double calc_A() {
  double ans = 1e100;
  for (double st_ = 0; st_ < all; st_ += _120) {
    double st = st_, ed = st + _120 + 0.1;
    while (ed - st > 1e-6) {
      double a = (st + st + ed) / 3;
      B_ = {r * cos(a), r * sin(a)};
      double val_a = (A_ - B_).nor() + dist(B, B_);
      double b = (st + ed + ed) / 3;
      B_ = {r * cos(b), r * sin(b)};
      double val_b = (A_ - B_).nor() + dist(B, B_);
      if (val_a < val_b)
        ed = b;
      else
        st = a;
    }
    double al = (st + ed) / 2;
    B_ = {r * cos(al), r * sin(al)};
    smin(ans, (A_ - B_).nor() + dist(B, B_));
  }
  return ans;
}
int main() {
  scanf("%lf %lf", &r, &h);
  F = point(0, 0, h);
  R = sqrt(r * r + h * h);
  A.input(), B.input();
  if (A.z > B.z) swap(A, B);
  double ans;
  if (B.z < 1e-6)
    ans = (A - B).nor();
  else if (A.z < 1e-6)
    A_ = A, ans = calc_A();
  else {
    ans = dist(A, B);
    for (double st_ = 0; st_ < all; st_ += _120) {
      double st = st_, ed = st + _120 + 0.1;
      while (ed - st > 1e-6) {
        double a = (st + st + ed) / 3;
        A_ = {r * cos(a), r * sin(a)};
        double val_a = dist(A, A_) + calc_A();
        double b = (st + ed + ed) / 3;
        A_ = {r * cos(b), r * sin(b)};
        double val_b = dist(A, A_) + calc_A();
        if (val_a < val_b)
          ed = b;
        else
          st = a;
      }
      double al = (st + ed) / 2;
      A_ = {r * cos(al), r * sin(al)};
      smin(ans, dist(A, A_) + calc_A());
    }
  }
  printf("%.6lf", ans);
}
