#include <bits/stdc++.h>
using namespace std;
const int oo = 999999999;
const double PI = 3.1415931;
int main() {
  ios_base::sync_with_stdio(false);
  int n, x1, x2, y1, y2;
  int ans = 0;
  cin >> n >> x1 >> y1 >> x2 >> y2;
  if (x1 == x2 && (x1 == 0 || x2 == n)) {
    cout << fabs(y1 - y2);
  } else if (y1 == y2 && (y1 == 0 || y2 == n)) {
    cout << fabs(x1 - x2);
  } else if (x1 == 0 || x2 == 0) {
    ans = min(int((n - y1) + fabs(x1 - x2) + (n - y2)),
              int(y1 + fabs(x1 - x2) + y2));
    cout << ans;
  } else {
    ans = min(int((n - x1) + fabs(y1 - y2) + (n - x2)),
              int(x1 + fabs(y1 - y2) + x2));
    cout << ans;
  }
  int _;
  cin >> _;
  return 0;
}
