#include <bits/stdc++.h>
using namespace std;
int main() {
  int t, n, x1, y1, x2, y2, dis, dis2;
  cin >> n >> x1 >> y1 >> x2 >> y2;
  if (x1 == x2 && y1 == y2) {
    cout << 0 << endl;
    return 0;
  }
  if (x2 == x1) {
    if (x1 == 0 || x1 == n) {
      dis = (y2 - y1 > 0) ? (y2 - y1) : (y1 - y2);
    }
    if ((double)(x1) >= (double)(n) / 2.0) {
      if (y2 > y1)
        dis = 2 * (n - x2) + (y2 - y1);
      else
        dis = 2 * (n - x2) + (y1 - y2);
    } else {
      if (y2 > y1)
        dis = 2 * (x2) + (y2 - y1);
      else
        dis = 2 * (x2) + (y1 - y2);
    }
    cout << dis << endl;
    return 0;
  }
  if (x2 < x1) {
    t = y1;
    y1 = y2;
    y2 = t;
    t = x1;
    x1 = x2;
    x2 = t;
  }
  if (y1 == y2) {
    dis = (n - y1) + (n - y2) + (x2 - x1);
    dis2 = (y1 + y2) + (x2 - x1);
  }
  if (y2 > y1) {
    dis = (n - y2) + (n - y1) + (x2 - x1) + 2 * (x1);
    dis2 = (y1 + y2) + 2 * (n - x2) + (x2 - x1);
  } else if (y2 < y1) {
    dis = (n - y1) + (n - y2) + 2 * (n - x2) + (x2 - x1);
    dis2 = (y1 + y2) + x2 - x1 + 2 * (x1);
  }
  if (dis < dis2)
    cout << dis << endl;
  else
    cout << dis2 << endl;
  return 0;
}
