#include <bits/stdc++.h>
using namespace std;
long long n, a, b, c, d, xx, yy, ans, mx = 1e9;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> a >> b >> c >> d;
  if (a == c && (a == 0 || a == n)) cout << abs(b - d) << endl, exit(0);
  if (b == d && (b == 0 || b == n)) cout << abs(a - c) << endl, exit(0);
  xx = 0, yy = 0;
  ans = abs(xx - a) + abs(xx - c) + abs(yy - b) + abs(yy - d);
  mx = min(mx, ans);
  xx = n, yy = 0;
  ans = abs(xx - a) + abs(xx - c) + abs(yy - b) + abs(yy - d);
  mx = min(mx, ans);
  xx = n, yy = n;
  ans = abs(xx - a) + abs(xx - c) + abs(yy - b) + abs(yy - d);
  mx = min(mx, ans);
  xx = 0, yy = n;
  ans = abs(xx - a) + abs(xx - c) + abs(yy - b) + abs(yy - d);
  mx = min(mx, ans);
  cout << mx << endl;
  return 0;
}
