#include <bits/stdc++.h>
using namespace std;
int main() {
  int i, j, k, n, m, t, p, c, x1, y1, x2, y2, cas = 1;
  while (scanf("%d %d %d %d %d", &n, &x1, &y1, &x2, &y2) == 5) {
    if ((x1 == 0 && y1 == 0) || (x2 == 0 && y2 == 0)) {
      if (x1 == 0 && y1 == 0)
        printf("%d\n", x2 + y2);
      else
        printf("%d\n", x1 + y1);
    } else if (x1 == x2 && (x1 == 0 || x1 == n)) {
      printf("%d\n", abs(y1 - y2));
    } else if (y1 == y2 && (y1 == 0 || y1 == n)) {
      printf("%d\n", abs(x1 - x2));
    } else if (x1 != 0 && y1 != 0 && x2 != 0 && y2 != 0) {
      i = x1 + y1 + x2 + y2;
      j = (n - x1) + (n - y2);
      k = (n - y1) + (n - x2);
      printf("%d\n", min(i, max(j, k)));
    } else if (x1 == 0 && y2 == 0) {
      printf("%d\n", x2 + y1);
    } else if (x2 == 0 && y1 == 0) {
      printf("%d\n", x1 + y2);
    } else if (x1 == 0 && y2 == n) {
      printf("%d\n", (n - y1) + x2);
    } else if (x2 == 0 && y1 == n) {
      printf("%d\n", (n - y2) + x1);
    } else if (y1 == 0 && x2 == n) {
      printf("%d\n", (n - x1) + y2);
    } else if (y2 == 0 && x1 == n) {
      printf("%d\n", (n - x2) + y1);
    } else if (x1 == 0 && x2 == n) {
      i = y1 + n + y2;
      j = (n - y1) + n + (n - y2);
      printf("%d\n", min(i, j));
    } else if (x2 == 0 && x1 == n) {
      i = y2 + n + y1;
      j = (n - y2) + n + (n - y1);
      printf("%d\n", min(i, j));
    } else if (y1 == 0 && y2 == n) {
      i = x1 + n + x2;
      j = (n - x1) + n + (n - x2);
      printf("%d\n", min(i, j));
    } else if (y2 == 0 && y1 == n) {
      i = x2 + n + x1;
      j = (n - x2) + n + (n - x1);
      printf("%d\n", min(i, j));
    }
  }
  return 0;
}
