#include <bits/stdc++.h>
const long long int mod = 1000000007;
using namespace std;
void toh_chaliye_shuru_karte_hain() {}
void code_samapti_ki_ghoshna() {}
long long int power(long long int a, long long int b, long long int modi) {
  a %= modi;
  long long int res = 1;
  while (b) {
    if (b % 2) {
      res = (res * a) % modi;
    }
    b /= 2;
    a = (a * a) % modi;
  }
  return res;
}
int main() {
  toh_chaliye_shuru_karte_hain();
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, x1, y1, x2, y2;
  cin >> n >> x1 >> y1 >> x2 >> y2;
  if (x1 == 0 && x2 == 0 || x1 == n && x2 == n) {
    cout << abs(y1 - y2) << "\n";
  } else if (y1 == 0 && y2 == 0 || y1 == n && y2 == n) {
    cout << abs(x1 - x2) << "\n";
  } else {
    int ans = 0;
    ans += min(n - y1 + n - y2, y1 + y2);
    ans += min(n - x1 + n - x2, x1 + x2);
    cout << ans << "\n";
  }
  code_samapti_ki_ghoshna();
  return 0;
}
