#include <bits/stdc++.h>
using namespace std;
long long int i, j, x, y, k = 0, l;
const int MOD = 1e9 + 7;
long long int m1 = LLONG_MAX;
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.second < b.second);
}
vector<long long int> g[1000001];
long long int n;
long long int v[101];
long long int s[101];
long long int m2 = LLONG_MIN;
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int n, x1, y1, x2, y2;
  cin >> n >> x1 >> y1 >> x2 >> y2;
  if (x1 == 0 && y1 >= 0 && x2 >= 0 && y2 == 0)
    cout << y1 + x2 << endl;
  else if (x2 == 0 && y2 >= 0 && x1 >= 0 && y1 == 0)
    cout << y2 + x1 << endl;
  else if (x1 >= 0 && y1 == 0 && x2 == n && y2 >= 0)
    cout << n - x1 + y2 << endl;
  else if (x2 >= 0 && y2 == 0 && x1 == n && y1 >= 0)
    cout << n - x2 + y1 << endl;
  else if (x1 == n && y1 >= 0 && y2 == n && x2 >= 0)
    cout << n - y1 + n - x2 << endl;
  else if (x2 == n && y2 >= 0 && y1 == n && x1 >= 0)
    cout << n - y2 + n - x1 << endl;
  else if (y1 == n && x1 >= 0 && x2 == 0 && y2 >= 0)
    cout << x1 + n - y2 << endl;
  else if (y2 == n && x2 >= 0 && x1 == 0 && y1 >= 0)
    cout << x2 + n - y1 << endl;
  else if ((x1 == 0 && y1 >= 0 && x2 == n && y2 >= 0) ||
           (x2 == 0 && y2 >= 0 && x1 == n && y1 >= 0))
    cout << min(4 * n - n - y1 - y2, n + y1 + y2) << endl;
  else if ((x1 >= 0 && y1 == 0 && y2 == n && x2 >= 0) ||
           (x2 >= 0 && y2 == 0 && y1 == n && x1 >= 0))
    cout << min(4 * n - n - x1 - x2, n + x1 + x2) << endl;
  else
    cout << max(abs(x1 - x2), abs(y1 - y2)) << endl;
}
