#include <bits/stdc++.h>
using namespace std;
long long powm(long long base, long long exp, long long mod = 1000000007) {
  long long ans = 1;
  while (exp) {
    if (exp & 1) ans = (ans * base) % mod;
    exp >>= 1, base = (base * base) % mod;
  }
  return ans;
}
void fastscan(int &x) {
  register int c = getchar();
  x = 0;
  for (; (c < 48 || c > 57); c = getchar())
    ;
  for (; c > 47 && c < 58; c = getchar()) {
    x = (x << 1) + (x << 3) + c - 48;
  }
}
const int N = 2e3 + 5;
struct cord {
  long long x, y;
} pt[3][3];
long long visited[3][3];
long long calc(long long a, long long b, long long c, long long d) {
  return sqrt((b - d) * (b - d) + (a - c) * (a - c));
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  pt[0][2].x = 0, pt[0][2].y = n;
  pt[2][0].x = 0, pt[2][0].y = 0;
  pt[1][2].x = n, pt[1][2].y = 0;
  pt[2][1].x = n, pt[2][1].y = n;
  long long x1, x2, y1, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  long long x = x1, y = y1, dist = 0;
  while (1) {
    if ((x == x2 and (x == 0 or x == n)) or (y == y2 and (y == 0 or y == n))) {
      dist += calc(x, y, x2, y2);
      x = x2, y = y2;
      break;
    } else if (x == 0 and !visited[0][2]) {
      dist += calc(x, y, pt[0][2].x, pt[0][2].y);
      x = pt[0][2].x, y = pt[0][2].y;
      visited[0][2] = 1;
    } else if (y == 0 and !visited[2][0]) {
      dist += calc(x, y, pt[2][0].x, pt[2][0].y);
      x = pt[2][0].x, y = pt[2][0].y;
      visited[2][0] = 1;
    } else if (x == n and !visited[1][2]) {
      dist += calc(x, y, pt[1][2].x, pt[1][2].y);
      x = pt[1][2].x, y = pt[1][2].y;
      visited[1][2] = 1;
    } else if (y == n and !visited[2][1]) {
      dist += calc(x, y, pt[2][1].x, pt[2][1].y);
      x = pt[2][1].x, y = pt[2][1].y;
      visited[2][1] = 1;
    }
  }
  cout << min(dist, 4 * n - dist) << endl;
}
