#include <bits/stdc++.h>
using namespace std;
int point[10][2], c[7][7], n, res;
int main() {
  scanf("%d %d %d %d %d", &n, &point[5][0], &point[5][1], &point[6][0],
        &point[6][1]);
  point[1][0] = 0;
  point[1][1] = 0;
  point[2][0] = n;
  point[2][1] = 0;
  point[3][0] = n;
  point[3][1] = n;
  point[4][0] = 0;
  point[4][1] = n;
  for (int i = 1; i <= 6; i++)
    for (int k = 1; k <= 6; k++) c[i][k] = 1000000;
  ;
  for (int i = 1; i <= 6; i++)
    for (int k = 1; k <= 6; k++) {
      if (point[i][0] == point[k][0] && (point[i][0] == 0 || point[i][0] == n))
        c[i][k] = abs(point[i][1] - point[k][1]);
      if (point[i][1] == point[k][1] && (point[i][1] == 0 || point[i][1] == n))
        c[i][k] = abs(point[i][0] - point[k][0]);
    }
  for (int k = 1; k <= 6; k++)
    for (int i = 1; i <= 6; i++)
      for (int m = 1; m <= 6; m++)
        if (c[i][m] > c[i][k] + c[k][m]) c[i][m] = c[i][k] + c[k][m];
  printf("%d", c[5][6]);
  return 0;
}
