#include <bits/stdc++.h>
using namespace std;
int d[1008][1008];
int main() {
  int n, x1, x2, y1, y2;
  int di[] = {0, 0, 1, -1};
  int dj[] = {1, -1, 0, 0};
  cin >> n >> x1 >> y1 >> x2 >> y2;
  int i, j, k;
  d[n - y1][x1] = 1;
  queue<int> qi, qj, dist;
  qi.push(n - y1);
  qj.push(x1);
  dist.push(1);
  int ii, jj, D;
  while (!qi.empty()) {
    ii = qi.front();
    jj = qj.front();
    qi.pop();
    qj.pop();
    D = dist.front();
    dist.pop();
    int t1, t2;
    for (k = 0; k < 4; k++) {
      t1 = ii + di[k];
      t2 = jj + dj[k];
      if ((t1 == 0 || t1 == n || t2 == 0 || t2 == n) && d[t1][t2] == 0 &&
          (t1 >= 0 && t1 <= n && t2 >= 0 && t2 <= n)) {
        d[t1][t2] = D + 1;
        qi.push(t1);
        qj.push(t2);
        dist.push(D + 1);
      }
    }
  }
  cout << d[n - y2][x2] - 1 << endl;
  return 0;
}
