#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;
}
