#include <bits/stdc++.h>
using namespace std;
int a[105][105];
int n;
int di(pair<int, int> first, pair<int, int> second) {
  if (first.first == second.first && (first.first == n || first.first == 0))
    return abs(first.second - second.second);
  if (first.second == second.second && (first.second == n || first.second == 0))
    return abs(first.first - second.first);
  return 55555555;
}
int main() {
  map<pair<int, int>, int> in;
  map<int, pair<int, int> > rev;
  int x1, x2, y1, y2;
  pair<int, int> cur;
  scanf("%d%d%d%d%d", &n, &x1, &y1, &x2, &y2);
  vector<pair<int, int> > po;
  po.push_back(make_pair(0, 0));
  po.push_back(make_pair(0, n));
  po.push_back(make_pair(n, 0));
  po.push_back(make_pair(n, n));
  po.push_back(make_pair(x1, y1));
  po.push_back(make_pair(x2, y2));
  int pp = 0;
  int i, j, k;
  for (i = 0; i < po.size(); i++) {
    cur = po[i];
    if (in.find(cur) == in.end()) {
      in[cur] = pp;
      rev[pp] = cur;
      pp++;
    }
  }
  for (i = 0; i < pp; i++)
    for (j = 0; j < pp; j++) a[i][j] = di(rev[i], rev[j]);
  for (k = 0; k < pp; k++)
    for (i = 0; i < pp; i++)
      for (j = 0; j < pp; j++)
        if (a[i][j] > a[i][k] + a[k][j]) {
          a[i][j] = a[j][i] = a[i][k] + a[k][j];
        }
  for (i = 0; i < pp; i++) {
    for (j = 0; j < pp; j++) {
    }
  }
  printf("%d\n", a[in[make_pair(x1, y1)]][in[make_pair(x2, y2)]]);
  return 0;
}
