#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 {
  long double x, y, z;
  inline point(long double x = 0, long double y = 0, long 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 point operator+(point a) { return point(x + a.x, y + a.y, z + a.z); }
  inline point operator*(long double a) { return point(x * a, y * a, z * a); }
  inline point operator/(long double a) { return point(x / a, y / a, z / a); }
  inline long double operator*(point a) { return x * a.x + y * a.y + z * a.z; }
  inline point norm() {
    long double s = nor();
    if (s < 1e-10) return *this;
    return point(x / s, y / s, z / s);
  }
  inline long double nor() { return sqrtl(nor2()); }
  inline long double nor2() { return *this * *this; }
  inline long double al() { return atan2l(y, x); }
  inline void input() { cin >> x >> y >> z; }
};
long double whole = 2 * 3.1415926535897932384626433832795, step = whole / 3;
long double r, h, R;
point A, AA, B;
inline long double calc_laterals(point a, point b) {
  long double al = fabsl(a.al() - b.al());
  smin(al, whole - al);
  long double dada = (a - point(0, 0, h)).nor2(), da = sqrtl(dada);
  long double dbdb = (b - point(0, 0, h)).nor2(), db = sqrtl(dbdb);
  return sqrtl(dada + dbdb - 2 * da * db * cosl(r * al / R));
}
inline long double solve() {
  long double ans = 1e13;
  for (long double sst = 0; sst < whole; sst += step) {
    long double st = sst, ed = st + step + 0.1;
    while (ed - st > 1e-10) {
      long double a = (st + st + ed) / 3, b = (st + ed + ed) / 3;
      point aa = point(r * cosl(a), r * sinl(a));
      long double sa = (AA - aa).nor() + calc_laterals(aa, B);
      point bb = point(r * cosl(b), r * sinl(b));
      long double sb = (AA - bb).nor() + calc_laterals(bb, B);
      if (sa > sb)
        st = a;
      else
        ed = b;
    }
    point aa = point(r * cosl(st), r * sinl(st));
    smin(ans, (AA - aa).nor() + calc_laterals(aa, B));
  }
  return ans;
}
int main() {
  cin >> r >> h;
  R = sqrtl(r * r + h * h);
  A.input(), B.input();
  if (B.z < 1e-10) swap(A, B);
  long double ans;
  if (A.z < 1e-10) {
    if (B.z < 1e-10)
      ans = (A - B).nor();
    else
      AA = A, ans = solve();
  } else {
    ans = calc_laterals(A, B);
    for (long double sst = 0; sst < whole; sst += step) {
      long double st = sst, ed = st + step + 0.1;
      while (ed - st > 1e-10) {
        long double a = (st + st + ed) / 3, b = (st + ed + ed) / 3;
        AA = point(r * cosl(a), r * sinl(a));
        long double sa = calc_laterals(A, AA) + solve();
        AA = point(r * cosl(b), r * sinl(b));
        long double sb = calc_laterals(A, AA) + solve();
        if (sa > sb)
          st = a;
        else
          ed = b;
      }
      AA = point(r * cosl(st), r * sinl(st));
      smin(ans, calc_laterals(A, AA) + solve());
    }
  }
  printf("%.7lf", (double)ans);
}
