#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <cmath>
#include <map>

using namespace std;

#define f first
#define s second
#define sz(v) int((v).size())

typedef long long ll;
typedef pair <int, int> pt;

const int N = 100;

int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};

int n;
int a[N + 1];
pt p, t;
map <pt, int> mp;
queue < pair <pt, int> > q;

int main(){

	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);

		cin >> n;
		for (int i = 1; i <= n; i++){
			cin >> a[i];
			for (int j = 1; j <= a[i]; j++)
				mp[make_pair(i, j)] = 1;
			mp[make_pair(i, a[i] + 1)] = 1;
		}
		cin >> p.f >> p.s;
		cin >> t.f >> t.s;
		if (p.f > t.f)
			swap(p, t);
		q.push(make_pair(p, 0));
		while (!q.empty()){
			pt v = q.front().f;
			int d = q.front().s;
			//cout << v.f << " " << v.s << " " << d << endl;
			if (v == t){
				cout << d;
				return 0;
			}
			q.pop();
			for (int i = 0; i < 4; i++){
				int x = v.f + dx[i];
				int y = v.s + dy[i];
				if (i == 3){
					if (x <= n){
						if (mp[make_pair(x, y)] == 0){
							if (mp[make_pair(x, a[x] + 1)] == 1){
								q.push(make_pair(make_pair(x, a[x] + 1), d + 1));
								mp[make_pair(x, a[x] + 1)] = 2;
							}
						}
						else{
							if (mp[make_pair(x, y)] == 1){
								q.push(make_pair(make_pair(x, y), d + 1));
								mp[make_pair(x, y)] = 2;
							}
						}
					}
				}
				if (i == 0){
					if (x >= 1){
						if (mp[make_pair(x, y)] == 0){
							if (mp[make_pair(x, a[x] + 1)] == 1){
								q.push(make_pair(make_pair(x, a[x] + 1), d + 1));
								mp[make_pair(x, a[x] + 1)] = 2;
							}
						}
						else{
							if (mp[make_pair(x, y)] == 1){
								q.push(make_pair(make_pair(x, y), d + 1));
								mp[make_pair(x, y)] = 2;
							}
						}
					}
				}
				if (i == 1){
					if (mp[make_pair(x, y)] == 1){
						q.push(make_pair(make_pair(x, y), d + 1));
						mp[make_pair(x, y)] = 2;
					}
				}
				if (i == 2){
					if (mp[make_pair(x, y)] == 1){
						q.push(make_pair(make_pair(x, y), d + 1));
						mp[make_pair(x, y)] = 2;
					}
				}
			}
		}

	return 0;

}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <cmath>
#include <map>

using namespace std;

#define f first
#define s second
#define sz(v) int((v).size())

typedef long long ll;
typedef pair <int, int> pt;

const int N = 100;

int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};

int n;
pt p, t;
int a[N + 1];
bool c[N + 1][int(1e5) + 1];
queue < pair <pt, int> > q;

int main(){

	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);

		cin >> n;
		for (int i = 1; i <= n; i++){
			cin >> a[i];
		}
		cin >> p.f >> p.s;
		cin >> t.f >> t.s;
		q.push(make_pair(p, 0));
		while (!q.empty()){
			pt v = q.front().f;
			int d = q.front().s;
			if (v == t){
				cout << d;
				return 0;
			}
			q.pop();
			for (int i = 0; i < 4; i++){
				int x = v.f + dx[i];
				int y = v.s + dy[i];
				if (i == 0 || i == 3){
					if (x >= 1 && x <= n){
						if (y > a[x] + 1){
							if (!c[x][a[x] + 1]){
								q.push(make_pair(make_pair(x, a[x] + 1), d + 1));
								c[x][a[x] + 1] = 1;
							}
						}
						else{
							if (!c[x][y]){
								q.push(make_pair(make_pair(x, y), d + 1));
								c[x][y] = 1;
							}
						}
					}
				}
				if (i == 1 || i == 2){
					if (x >= 1 && x <= n && y >= 1 && y <= a[x] + 1 && !c[x][y]){
						q.push(make_pair(make_pair(x, y), d + 1));
						c[x][y] = 1;
					}
				}
			}
		}

	return 0;

}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <cmath>
#include <map>

using namespace std;

#define f first
#define s second
#define sz(v) int((v).size())

typedef long long ll;
typedef pair <int, int> pt;

const int N = 100;
const int inf = int(1e9);

int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};

int n;
int a[N + 1];
pt p, t;
int used[N + 5][N + 5];
queue < pair <pt, int> > q;

int main(){

	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);

		memset(used, -1, sizeof(used));
		cin >> n;
		for (int i = 1; i <= n; i++){
			cin >> a[i];
			for (int j = 1; j <= a[i]; j++)
				used[i][j] = 0;
			used[i][a[i] + 1] = 0;
		}
		cin >> p.f >> p.s;
		cin >> t.f >> t.s;
		q.push(make_pair(p, 0));
		while (!q.empty()){
			pt v = q.front().f;
			int d = q.front().s;
			if (v == t){
				cout << d;
				return 0;
			}
			q.pop();
			for (int i = 0; i < 4; i++){
				int x = v.f + dx[i];
				int y = v.s + dy[i];
				if (i == 1){
					if (x >= 1){
						if (!used[x][y]){
							q.push(make_pair(make_pair(x, y), d + 1));
							used[x][y] = 1;
						}
					}
				}
				if (i == 2){
					if (x <= n){
						if (!used[x][y]){
							q.push(make_pair(make_pair(x, y), d + 1));
							used[x][y] = 1;
						}
					}
				}
				if (i == 0){
					if (x >= 1){
						if (used[x][y] == -1){
							if (!used[x][a[x] + 1]){
								q.push(make_pair(make_pair(x, a[x] + 1), d + 1));
								used[x][a[x] + 1] = 1;
							}
						}
						else{
							if (!used[x][y]){
								q.push(make_pair(make_pair(x, y), d + 1));
								used[x][y] = 1;
							}
						}
					}
				}
				if (i == 3){
					if (x <= n){
						if (used[x][y] == -1){
							if (!used[x][a[x] + 1]){
								q.push(make_pair(make_pair(x, a[x] + 1), d + 1));
								used[x][a[x] + 1] = 1;
							}
						}
						else{
							if (!used[x][y]){
								q.push(make_pair(make_pair(x, y), d + 1));
								used[x][y] = 1;
							}
						}
					}
				}
			}
		}

	return 0;

}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <cmath>
#include <map>

using namespace std;

#define f first
#define s second
#define sz(v) int((v).size())

typedef long long ll;
typedef pair <int, int> pt;

const int N = 100;

int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};

int n;
int a[N + 1];
pt p, t;
bool c[N + 3][int(1e5) + 3];
queue < pair <pt, int> > q;

int main(){

	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);

		cin >> n;
		for (int i = 1; i <= n; i++){
			cin >> a[i];
		}
		cin >> p.f >> p.s;
		cin >> t.f >> t.s;
		q.push(make_pair(p, 0));
		while (!q.empty()){
			pt v = q.front().f;
			int d = q.front().s;
			//cout << v.f << " " << v.s << " " << d << endl;
			if (v == t){
				cout << d;
				return 0;
			}
			q.pop();
			for (int i = 0; i < 4; i++){
				int x = v.f + dx[i];
				int y = v.s + dy[i];
				if (i == 3){
					if (x <= n){
						if (y > a[x] + 1){
							if (!c[x][a[x] + 1]){
								q.push(make_pair(make_pair(x, a[x] + 1), d + 1));
								c[x][a[x] + 1] = 1;
							}
						}
						else{
							if (!c[x][y]){
								q.push(make_pair(make_pair(x, y), d + 1));
								c[x][y] = 1;
							}
						}
					}
				}
				if (i == 0){
					if (x >= 1){
						if (y > a[x] + 1){
							if (!c[x][a[x] + 1]){
								q.push(make_pair(make_pair(x, a[x] + 1), d + 1));
								c[x][a[x] + 1] = 1;
							}
						}
						else{
							if (!c[x][y]){
								q.push(make_pair(make_pair(x, y), d + 1));
								c[x][y] = 1;
							}
						}
					}
				}
				if (i == 1 || i == 2){
					if (x >= 1 && x <= n && y >= 1 && y <= a[x] + 1 && !c[x][y]){
						q.push(make_pair(make_pair(x, y), d + 1));
						c[x][y] = 1;
					}
				}
			}
		}

	return 0;

}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <cmath>
#include <map>

using namespace std;

#define f first
#define s second
#define sz(v) int((v).size())

typedef long long ll;
typedef pair <int, int> pt;

const int N = 100;

int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};

int n;
int a[N + 1];
pt p, t;
map <pt, int> mp;
queue < pair <pt, int> > q;

int main(){

	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);

		cin >> n;
		for (int i = 1; i <= n; i++){
			cin >> a[i];
			for (int j = 1; j <= a[i]; j++)
				mp[make_pair(i, j)] = 1;
			mp[make_pair(i, a[i] + 1)] = 1;
		}
		cin >> p.f >> p.s;
		cin >> t.f >> t.s;
		q.push(make_pair(p, 0));
		while (!q.empty()){
			pt v = q.front().f;
			int d = q.front().s;
			if (v == t){
				cout << d;
				return 0;
			}
			q.pop();
			for (int i = 0; i < 4; i++){
				int x = v.f + dx[i];
				int y = v.s + dy[i];
				if (i == 1){
					if (x >= 1){
						if (mp[make_pair(x, y)] == 1){
							q.push(make_pair(make_pair(x, y), d + 1));
							mp[make_pair(x, y)] = 2;
						}
					}
				}
				if (i == 2){
					if (x <= n){
						if (mp[make_pair(x, y)] == 1){
							q.push(make_pair(make_pair(x, y), d + 1));
							mp[make_pair(x, y)] = 2;
						}
					}
				}
				if (i == 0){
					if (x >= 1){
						if (mp[make_pair(x, y)] == 0){
							if (mp[make_pair(x, a[x] + 1)] == 1){
								q.push(make_pair(make_pair(x, a[x] + 1), d + 1));
								mp[make_pair(x, a[x] + 1)] = 2;
							}
						}
						else{
							if (mp[make_pair(x, y)] == 1){
								q.push(make_pair(make_pair(x, y), d + 1));
								mp[make_pair(x, y)] = 2;
							}
						}
					}
				}
				if (i == 3){
					if (x <= n){
						if (mp[make_pair(x, y)] == 0){
							if (mp[make_pair(x, a[x] + 1)] == 1){
								q.push(make_pair(make_pair(x, a[x] + 1), d + 1));
								mp[make_pair(x, a[x] + 1)] = 2;
							}
						}
						else{
							if (mp[make_pair(x, y)] == 1){
								q.push(make_pair(make_pair(x, y), d + 1));
								mp[make_pair(x, y)] = 2;
							}
						}
					}
				}
			}
		}

	return 0;

}


#pragma comment(linker,"/STACK:100000000,100000000")

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <stack>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <iomanip>
#include <queue>
#include <map>
#include <functional>
#include <memory.h>
#include <list>
#include <sstream>
#include <ctime>
#include <climits>

using namespace std;

/* Constants begin */
const long long inf = 1e18+7;
const long long mod = 1e9+7;
const double eps = 1e-10;
const double PI = 2*acos(0.0);
const double E = 2.71828;
/* Constants end */

/* Defines begin */
#define pb push_back
#define mp make_pair
#define ll long long
#define ld long double
#define F first
#define S second
#define all(a) (a).begin(), a.end()
#define forn(i,n) for (ll (i)=0;(i)<(ll)(n);(i)++)
#define ford(i,n) for (ll (i)=(n)-1;(i)>=(ll)0;(i)--)
#define foreach(itr,c) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
/* Defines end */

ll a[100005], se[1005][1005] ;

int main(void) {
 //   #ifndef ONLINE_JUDGE
        freopen("input.txt","rt",stdin);
        freopen("output.txt","wt",stdout);
 //   #endif
    ll n ; cin >> n ;
    forn(i,n) cin >> a[i] , se[i][i] = a[i] ;
    for(ll i = 0; i < n; i++)
    for(ll j = i + 1; j < n; j++)
        se[i][j] = se[j][i] = min( se[i][j-1], a[j] ) ;
    ll x, y, X, Y;
    cin >> x >> y >> X >> Y ;
    if( x > X ) swap(x,X) , swap(y,Y) ;
    x -- ; X -- ; y -- ; Y -- ;
    ll Min = inf ;
    for(ll i = 0; i < n; i++ ) {
        Min = min( abs( i - x ) + abs( X - i ) + abs( min(se[i][x],min(y,se[i][X])) - Y) , Min ) ;
        Min = min( abs( i - x ) + abs( X - i ) + abs( min(se[i][x],min(y,se[i][X])) - Y) , Min ) ;
    }
    cout << Min << endl ;
    return 0 ;
}


#pragma comment(linker,"/STACK:100000000,100000000")

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <stack>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <iomanip>
#include <queue>
#include <map>
#include <functional>
#include <memory.h>
#include <list>
#include <sstream>
#include <ctime>
#include <climits>

using namespace std;

/* Constants begin */
const long long inf = 1e18+7;
const long long mod = 1e9+7;
const double eps = 1e-10;
const double PI = 2*acos(0.0);
const double E = 2.71828;
/* Constants end */

/* Defines begin */
#define pb push_back
#define mp make_pair
#define ll long long
#define ld long double
#define F first
#define S second
#define all(a) (a).begin(), a.end()
#define forn(i,n) for (ll (i)=0;(i)<(ll)(n);(i)++)
#define ford(i,n) for (ll (i)=(n)-1;(i)>=(ll)0;(i)--)
#define foreach(itr,c) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
/* Defines end */

ll a[100005] ;

int main(void) {
 //   #ifndef ONLINE_JUDGE
        freopen("input.txt","rt",stdin);
        freopen("output.txt","wt",stdout);
 //   #endif
    ll n ; cin >> n ;
    forn(i,n) cin >> a[i] ;
    ll x, y, X, Y;
    cin >> x >> y >> X >> Y ;
    if( x > X ) swap(x,X) , swap(y,Y) ;
    x -- ; X -- ; y -- ; Y -- ;
    ll p = x , q = y ;
    ll rez = 0 ;
    ll ans = 0 ;
    ll Min = inf ;
    while( 1 ) {
        for(ll i = 0; i < n; i++ ) {
            Min = min( 2 * abs( i - x ) + abs(a[i] - Y) , Min ) ;
        }
        if( x == X ) {
            rez += Min ;
            ans = rez ;
            break ;
        }
        x ++ ; rez ++ ;
        if( a[x] < y ) y = a[x] ;
    }
    cout << ans << endl ;
    return 0 ;
}


#pragma comment(linker,"/STACK:100000000,100000000")

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <stack>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <iomanip>
#include <queue>
#include <map>
#include <functional>
#include <memory.h>
#include <list>
#include <sstream>
#include <ctime>
#include <climits>

using namespace std;

/* Constants begin */
const long long inf = 1e18+7;
const long long mod = 1e9+7;
const double eps = 1e-10;
const double PI = 2*acos(0.0);
const double E = 2.71828;
/* Constants end */

/* Defines begin */
#define pb push_back
#define mp make_pair
#define ll long long
#define ld long double
#define F first
#define S second
#define all(a) (a).begin(), a.end()
#define forn(i,n) for (ll (i)=0;(i)<(ll)(n);(i)++)
#define ford(i,n) for (ll (i)=(n)-1;(i)>=(ll)0;(i)--)
#define foreach(itr,c) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
/* Defines end */

ll a[100005] ;
ll qx[10000005], qy[10000005] ;
ll dx[]={0,1,0,-1};
ll dy[]={1,0,-1,0};
ll dist[105][100005] ;

int main(void) {
 //   #ifndef ONLINE_JUDGE
        freopen("input.txt","rt",stdin);
        freopen("output.txt","wt",stdout);
 //   #endif
    ll n ; cin >> n;
    forn(i,n) cin >> a[i] ;
    ll x, y, lx, ly ;
        cin >> x >> y >> lx >> ly ;
    x --; y --; lx --; ly --;
    ll qt, qh ; qt = qh = 0 ;
    qx[qt] = x; qy[qt] = y; qt++ ;
    memset(dist,-1,sizeof(dist)) ;
    dist[x][y] = 0;
    for(; qh < qt; qh++) {
        ll nx = qx[qh], ny = qy[qh] ;
        forn(dir,4) {
            x = nx + dx[dir];
            y = ny + dy[dir] ;
            if( x >= n || x < 0 ) continue;
            if( y < 0 ) continue;
            y = min(y,a[x]) ;
                if( dist[x][y] == -1 ) {
                    dist[x][y] = dist[nx][ny] + 1;
                    qx[qt] = x; qy[qt] = y; qt ++;
                }
        }
    }
    cout << dist[lx][ly] << endl ;
    return 0 ;
}


#pragma comment(linker,"/STACK:100000000,100000000")

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <stack>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <iomanip>
#include <queue>
#include <map>
#include <functional>
#include <memory.h>
#include <list>
#include <sstream>
#include <ctime>
#include <climits>

using namespace std;

/* Constants begin */
const long long inf = 1e18+7;
const long long mod = 1e9+7;
const double eps = 1e-10;
const double PI = 2*acos(0.0);
const double E = 2.71828;
/* Constants end */

/* Defines begin */
#define pb push_back
#define mp make_pair
#define ll long long
#define ld long double
#define F first
#define S second
#define all(a) (a).begin(), a.end()
#define forn(i,n) for (ll (i)=0;(i)<(ll)(n);(i)++)
#define ford(i,n) for (ll (i)=(n)-1;(i)>=(ll)0;(i)--)
#define foreach(itr,c) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
/* Defines end */

ll a[100005] ;

int main(void) {
 //   #ifndef ONLINE_JUDGE
        freopen("input.txt","rt",stdin);
        freopen("output.txt","wt",stdout);
 //   #endif
    ll n ; cin >> n ;
    forn(i,n) cin >> a[i] ;
    ll x, y, X, Y;
    cin >> x >> y >> X >> Y ;
    if( x > X ) swap(x,X) , swap(y,Y) ;
    x -- ; X -- ; y -- ; Y -- ;
    ll p = x , q = y ;
    ll rez = 0 ;
    ll ans = 0 ;
    while( 1 ) {
        if( x == X ) {
            ll Min = abs( y - Y ) ;
            for(ll i = 0; i <= n; i++ ) {
                Min = min( 2 * abs( i - x ) + abs(a[i] - Y) , Min ) ;
            }
            rez += Min ;
            ans = rez ;
            break ;
        }
        x ++ ; rez ++ ;
        if( a[x] < y ) y = a[x] ;
    }
    x = p , y = q ;
    swap(x,X) ; swap(y,Y) ;
    while( 1 ) {
       // cout << x << " " << y << endl ;
        if( x == X ) {
            ll Min = abs( y - Y )  ;
            for(ll i = 0; i <= n; i++ ) {
                Min = min( 2 * abs( i - x ) + abs(a[i] - Y) , Min ) ;
            }            cout << min(ans, rez) << endl ;
            exit(0) ;
        }
        x -- ; rez ++ ;
        if( a[x] < y ) y = a[x] ;
    }
    return 0 ;
}


#pragma comment(linker,"/STACK:100000000,100000000")

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <stack>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <iomanip>
#include <queue>
#include <map>
#include <functional>
#include <memory.h>
#include <list>
#include <sstream>
#include <ctime>
#include <climits>

using namespace std;

/* Constants begin */
const long long inf = 1e18+7;
const long long mod = 1e9+7;
const double eps = 1e-10;
const double PI = 2*acos(0.0);
const double E = 2.71828;
/* Constants end */

/* Defines begin */
#define pb push_back
#define mp make_pair
#define ll long long
#define ld long double
#define F first
#define S second
#define all(a) (a).begin(), a.end()
#define forn(i,n) for (ll (i)=0;(i)<(ll)(n);(i)++)
#define ford(i,n) for (ll (i)=(n)-1;(i)>=(ll)0;(i)--)
#define foreach(itr,c) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
/* Defines end */

ll a[100005] ;
ll qx[10000005], qy[10000005] ;
ll dx[]={0,1,0,-1};
ll dy[]={1,0,-1,0};
ll dist[105][100005] ;

int main(void) {
 //   #ifndef ONLINE_JUDGE
        freopen("input.txt","rt",stdin);
        freopen("output.txt","wt",stdout);
 //   #endif
    ll n ; cin >> n;
    forn(i,n) cin >> a[i] ;
    ll x, y, lx, ly ;
        cin >> x >> y >> lx >> ly ;
    x --; y --; lx --; ly --;
    ll qt, qh ; qt = qh = 0 ;
    qx[qt] = x; qy[qt] = y; qt++ ;
    memset(dist,-1,sizeof(dist)) ;
    dist[x][y] = 0;
    for(; qh < qt; qh++) {
        ll nx = qx[qh], ny = qy[qh] ;
        forn(dir,4) {
            x = nx + dx[dir];
            y = ny + dy[dir] ;
            if( x > n || x < 0 ) continue;
            if( y < 0 ) continue;
            y = min(y,a[x]) ;
                if( dist[x][y] == -1 ) {
                    dist[x][y] = dist[nx][ny] + 1;
                    qx[qt] = x; qy[qt] = y; qt ++;
                }
        }
    }
    cout << dist[lx][ly] << endl ;
    return 0 ;
}


#pragma comment(linker,"/STACK:100000000,100000000")

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <stack>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <iomanip>
#include <queue>
#include <map>
#include <functional>
#include <memory.h>
#include <list>
#include <sstream>
#include <ctime>
#include <climits>

using namespace std;

/* Constants begin */
const long long inf = 1e18+7;
const long long mod = 1e9+7;
const double eps = 1e-10;
const double PI = 2*acos(0.0);
const double E = 2.71828;
/* Constants end */

/* Defines begin */
#define pb push_back
#define mp make_pair
#define ll long long
#define ld long double
#define F first
#define S second
#define all(a) (a).begin(), a.end()
#define forn(i,n) for (ll (i)=0;(i)<(ll)(n);(i)++)
#define ford(i,n) for (ll (i)=(n)-1;(i)>=(ll)0;(i)--)
#define foreach(itr,c) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
/* Defines end */

ll a[100005] ;

int main(void) {
 //   #ifndef ONLINE_JUDGE
        freopen("input.txt","rt",stdin);
        freopen("output.txt","wt",stdout);
 //   #endif
    ll n ; cin >> n ;
    forn(i,n) cin >> a[i] ;
    ll x, y, X, Y;
    cin >> x >> y >> X >> Y ;
    if( x > X ) swap(x,X) , swap(y,Y) ;
    x -- ; X -- ; y -- ; Y -- ;
    ll p = x , q = y ;
    ll rez = 0 ;
    ll ans = 0 ;
    ll PP = inf ;
    for(ll i = 0; i < x; i++) {
        PP = min( PP, abs( Y - a[i] ) + 1 + 2 * abs( x  - i ) ) ;
     //   cout << PP << endl;
    }

    for(ll i = X + 1; i < n; i++) {
        PP = min( PP, abs( Y - a[i] ) + 1 + 2 * abs( X  - i ) ) ;
    }
    // cout << PP << endl ;
    while( 1 ) {
        if( x == X ) {
            rez += min(  PP , abs( y - Y ) ) ; ;
            ans = rez ;
            break ;
        }
        x ++ ; rez ++ ;
        if( a[x] < y ) y = a[x] ;
    }
    cout << ans << endl ;
    return 0 ;
}


#pragma comment(linker,"/STACK:100000000,100000000")

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <stack>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <iomanip>
#include <queue>
#include <map>
#include <functional>
#include <memory.h>
#include <list>
#include <sstream>
#include <ctime>
#include <climits>

using namespace std;

/* Constants begin */
const long long inf = 1e18+7;
const long long mod = 1e9+7;
const double eps = 1e-10;
const double PI = 2*acos(0.0);
const double E = 2.71828;
/* Constants end */

/* Defines begin */
#define pb push_back
#define mp make_pair
#define ll long long
#define ld long double
#define F first
#define S second
#define all(a) (a).begin(), a.end()
#define forn(i,n) for (ll (i)=0;(i)<(ll)(n);(i)++)
#define ford(i,n) for (ll (i)=(n)-1;(i)>=(ll)0;(i)--)
#define foreach(itr,c) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
/* Defines end */

ll a[100005] ;

int main(void) {
 //   #ifndef ONLINE_JUDGE
        freopen("input.txt","rt",stdin);
        freopen("output.txt","wt",stdout);
 //   #endif
    ll n ; cin >> n ;
    forn(i,n) cin >> a[i] ;
    ll x, y, X, Y;
    cin >> x >> y >> X >> Y ;
    if( x > X ) swap(x,X) , swap(y,Y) ;
    x -- ; X -- ; y -- ; Y -- ;
    ll p = x , q = y ;
    ll rez = 0 ;
    ll ans = 0 ;
    while( 1 ) {
        if( x == X ) {
            ll Min = abs( y - Y ) ;
            for(ll i = 0; i < n; i++ ) {
                Min = min( 2 * abs( i - x ) + abs(a[i] - Y) , Min ) ;
            }
            rez += Min ;
            ans = rez ;
            break ;
        }
        x ++ ; rez ++ ;
        if( a[x] < y ) y = a[x] ;
    }
    x = p , y = q ;
    swap(x,X) ; swap(y,Y) ;
    while( 1 ) {
       // cout << x << " " << y << endl ;
        if( x == X ) {
            ll Min = abs( y - Y )  ;
            for(ll i = 0; i < n; i++ ) {
                Min = min( 2 * abs( i - x ) + abs(a[i] - Y) , Min ) ;
            }            cout << min(ans, rez) << endl ;
            exit(0) ;
        }
        x -- ; rez ++ ;
        if( a[x] < y ) y = a[x] ;
    }
    return 0 ;
}


#pragma comment(linker,"/STACK:100000000,100000000")

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <stack>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <iomanip>
#include <queue>
#include <map>
#include <functional>
#include <memory.h>
#include <list>
#include <sstream>
#include <ctime>
#include <climits>

using namespace std;

/* Constants begin */
const long long inf = 1e18+7;
const long long mod = 1e9+7;
const double eps = 1e-10;
const double PI = 2*acos(0.0);
const double E = 2.71828;
/* Constants end */

/* Defines begin */
#define pb push_back
#define mp make_pair
#define ll long long
#define ld long double
#define F first
#define S second
#define all(a) (a).begin(), a.end()
#define forn(i,n) for (ll (i)=0;(i)<(ll)(n);(i)++)
#define ford(i,n) for (ll (i)=(n)-1;(i)>=(ll)0;(i)--)
#define foreach(itr,c) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
/* Defines end */

ll a[100005] ;

int main(void) {
 //   #ifndef ONLINE_JUDGE
        freopen("input.txt","rt",stdin);
        freopen("output.txt","wt",stdout);
 //   #endif
    ll n ; cin >> n ;
    forn(i,n) cin >> a[i] ;
    ll x, y, X, Y;
    cin >> x >> y >> X >> Y ;
    if( x > X ) swap(x,X) , swap(y,Y) ;
    x -- ; X -- ; y -- ; Y -- ;
    ll p = x , q = y ;
    ll rez = 0 ;
    ll ans = 0 ;
    ll PP = inf ;
    for(ll i = 0; i < x; i++) {
        PP = min( PP, abs( Y - a[i] ) + 2 * abs( x  - i ) ) ;
    }

    for(ll i = X; i < n; i++) {
        PP = min( PP, abs( Y - a[i] ) + 2 * abs( X  - i ) ) ;
    }

    while( 1 ) {
        if( x == X ) {
            rez += min(  PP , abs( y - Y ) ) ; ;
            ans = rez ;
            break ;
        }
        x ++ ; rez ++ ;
        if( a[x] < y ) y = a[x] ;
    }
    cout << ans << endl ;
    return 0 ;
}


#pragma comment(linker,"/STACK:100000000,100000000")

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <stack>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <iomanip>
#include <queue>
#include <map>
#include <functional>
#include <memory.h>
#include <list>
#include <sstream>
#include <ctime>
#include <climits>

using namespace std;

/* Constants begin */
const long long inf = 1e18+7;
const long long mod = 1e9+7;
const double eps = 1e-10;
const double PI = 2*acos(0.0);
const double E = 2.71828;
/* Constants end */

/* Defines begin */
#define pb push_back
#define mp make_pair
#define ll long long
#define ld long double
#define F first
#define S second
#define all(a) (a).begin(), a.end()
#define forn(i,n) for (ll (i)=0;(i)<(ll)(n);(i)++)
#define ford(i,n) for (ll (i)=(n)-1;(i)>=(ll)0;(i)--)
#define foreach(itr,c) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
/* Defines end */

ll a[100005] ;
ll qx[100005], qy[100005] ;
ll dx[]={0,1,0,-1};
ll dy[]={1,0,-1,0};
ll used[1005][1005], dist[1005][1005] ;

int main(void) {
 //   #ifndef ONLINE_JUDGE
        freopen("input.txt","rt",stdin);
        freopen("output.txt","wt",stdout);
 //   #endif
    ll n ; cin >> n;
    forn(i,n) cin >> a[i] ;
    ll x, y, lx, ly ;
        cin >> x >> y >> lx >> ly ;
    x --; y --; lx --; ly --;
    ll qt, qh ; qt = qh = 0 ;
    qx[qt] = x; qy[qt] = y; qt++ ;
    used[x][y] = 1;
    for(; qh < qt; qh++) {
        ll nx = qx[qh], ny = qy[qh] ;
        forn(dir,4) {
            x = nx + dx[dir];
            y = ny + dy[dir] ;
            if( x > n || x < 0 ) continue;
            if( y < 0 ) continue;
            y = min(y,a[x]) ;
                if( !used[x][y] ) {
                    dist[x][y] = dist[nx][ny] + 1;
                    used[x][y] = 1;
                    qx[qt] = x; qy[qt] = y; qt ++;
                }
        }
    }
    cout << dist[lx][ly] << endl ;
    return 0 ;
}


#pragma comment(linker,"/STACK:100000000,100000000")

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <stack>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <iomanip>
#include <queue>
#include <map>
#include <functional>
#include <memory.h>
#include <list>
#include <sstream>
#include <ctime>
#include <climits>

using namespace std;

/* Constants begin */
const long long inf = 1e18+7;
const long long mod = 1e9+7;
const double eps = 1e-10;
const double PI = 2*acos(0.0);
const double E = 2.71828;
/* Constants end */

/* Defines begin */
#define pb push_back
#define mp make_pair
#define ll long long
#define ld long double
#define F first
#define S second
#define all(a) (a).begin(), a.end()
#define forn(i,n) for (ll (i)=0;(i)<(ll)(n);(i)++)
#define ford(i,n) for (ll (i)=(n)-1;(i)>=(ll)0;(i)--)
#define foreach(itr,c) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
/* Defines end */

ll a[100005] ;
ll qx[10000005], qy[10000005] ;
ll dx[]={0,1,0,-1};
ll dy[]={1,0,-1,0};
ll used[105][100005], dist[105][100005] ;

int main(void) {
 //   #ifndef ONLINE_JUDGE
        freopen("input.txt","rt",stdin);
        freopen("output.txt","wt",stdout);
 //   #endif
    ll n ; cin >> n;
    forn(i,n) cin >> a[i] ;
    ll x, y, lx, ly ;
        cin >> x >> y >> lx >> ly ;
    x --; y --; lx --; ly --;
    ll qt, qh ; qt = qh = 0 ;
    qx[qt] = x; qy[qt] = y; qt++ ;
    used[x][y] = 1;
    for(; qh < qt; qh++) {
        ll nx = qx[qh], ny = qy[qh] ;
        forn(dir,4) {
            x = nx + dx[dir];
            y = ny + dy[dir] ;
            if( x > n || x < 0 ) continue;
            if( y < 0 ) continue;
            y = min(y,a[x]) ;
                if( !used[x][y] ) {
                    dist[x][y] = dist[nx][ny] + 1;
                    used[x][y] = 1;
                    qx[qt] = x; qy[qt] = y; qt ++;
                }
        }
    }
    cout << dist[lx][ly] << endl ;
    return 0 ;
}


#pragma comment(linker,"/STACK:100000000,100000000")

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <stack>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <iomanip>
#include <queue>
#include <map>
#include <functional>
#include <memory.h>
#include <list>
#include <sstream>
#include <ctime>
#include <climits>

using namespace std;

/* Constants begin */
const long long inf = 1e18+7;
const long long mod = 1e9+7;
const double eps = 1e-10;
const double PI = 2*acos(0.0);
const double E = 2.71828;
/* Constants end */

/* Defines begin */
#define pb push_back
#define mp make_pair
#define ll long long
#define ld long double
#define F first
#define S second
#define all(a) (a).begin(), a.end()
#define forn(i,n) for (ll (i)=0;(i)<(ll)(n);(i)++)
#define ford(i,n) for (ll (i)=(n)-1;(i)>=(ll)0;(i)--)
#define foreach(itr,c) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
/* Defines end */

ll a[100005] ;
ll qx[10000005], qy[10000005] ;
ll dx[]={0,1,0,-1};
ll dy[]={1,0,-1,0};
ll dist[105][100005] ;

int main(void) {
 //   #ifndef ONLINE_JUDGE
        freopen("input.txt","rt",stdin);
        freopen("output.txt","wt",stdout);
 //   #endif
    ll n ; cin >> n;
    forn(i,n) cin >> a[i] ;
    ll x, y, lx, ly ;
        cin >> x >> y >> lx >> ly ;
    x --; y --; lx --; ly --;
    ll qt, qh ; qt = qh = 0 ;
    qx[qt] = x; qy[qt] = y; qt++ ;
    memset(dist,-1,sizeof(dist)) ;
    for(; qh < qt; qh++) {
        ll nx = qx[qh], ny = qy[qh] ;
        forn(dir,4) {
            x = nx + dx[dir];
            y = ny + dy[dir] ;
            if( x > n || x < 0 ) continue;
            if( y < 0 ) continue;
            y = min(y,a[x]) ;
                if( dist[x][y] == -1 ) {
                    dist[x][y] = dist[nx][ny] + 1;
                    qx[qt] = x; qy[qt] = y; qt ++;
                }
        }
    }
    cout << dist[lx][ly] << endl ;
    return 0 ;
}


#pragma comment(linker,"/STACK:100000000,100000000")

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <stack>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <iomanip>
#include <queue>
#include <map>
#include <functional>
#include <memory.h>
#include <list>
#include <sstream>
#include <ctime>
#include <climits>

using namespace std;

/* Constants begin */
const long long inf = 1e18+7;
const long long mod = 1e9+7;
const double eps = 1e-10;
const double PI = 2*acos(0.0);
const double E = 2.71828;
/* Constants end */

/* Defines begin */
#define pb push_back
#define mp make_pair
#define ll long long
#define ld long double
#define F first
#define S second
#define all(a) (a).begin(), a.end()
#define forn(i,n) for (ll (i)=0;(i)<(ll)(n);(i)++)
#define ford(i,n) for (ll (i)=(n)-1;(i)>=(ll)0;(i)--)
#define foreach(itr,c) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
/* Defines end */

ll a[100005], se[1005][1005] ;

int main(void) {
 //   #ifndef ONLINE_JUDGE
        freopen("input.txt","rt",stdin);
        freopen("output.txt","wt",stdout);
 //   #endif
    ll n ; cin >> n ;
    forn(i,n) cin >> a[i] , se[i][i] = a[i] ;
    for(ll i = 0; i < n; i++)
    for(ll j = i + 1; j < n; j++)
        se[i][j] = se[j][i] = min( se[i][j-1], a[j] ) ;
    ll x, y, X, Y;
    cin >> x >> y >> X >> Y ;
    if( x > X ) swap(x,X) , swap(y,Y) ;
    x -- ; X -- ; y -- ; Y -- ;
    ll Min = inf ;
    for(ll i = 0; i < n; i++ ) {
        Min = min( abs( i - x ) + abs( X - i ) + abs( min(se[i][x],y) - Y) , Min ) ;
    }
    cout << Min << endl ;
    return 0 ;
}


#pragma comment(linker,"/STACK:100000000,100000000")

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <stack>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <iomanip>
#include <queue>
#include <map>
#include <functional>
#include <memory.h>
#include <list>
#include <sstream>
#include <ctime>
#include <climits>

using namespace std;

/* Constants begin */
const long long inf = 1e18+7;
const long long mod = 1e9+7;
const double eps = 1e-10;
const double PI = 2*acos(0.0);
const double E = 2.71828;
/* Constants end */

/* Defines begin */
#define pb push_back
#define mp make_pair
#define ll long long
#define ld long double
#define F first
#define S second
#define all(a) (a).begin(), a.end()
#define forn(i,n) for (ll (i)=0;(i)<(ll)(n);(i)++)
#define ford(i,n) for (ll (i)=(n)-1;(i)>=(ll)0;(i)--)
#define foreach(itr,c) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
/* Defines end */

ll a[100005] ;

int main(void) {
 //   #ifndef ONLINE_JUDGE
        freopen("input.txt","rt",stdin);
        freopen("output.txt","wt",stdout);
 //   #endif
    ll n ; cin >> n ;
    forn(i,n) cin >> a[i] ;
    ll x, y, X, Y;
    cin >> x >> y >> X >> Y ;
    if( x > X ) swap(x,X) , swap(y,Y) ;
    x -- ; X -- ; y -- ; Y -- ;
    ll p = x , q = y ;
    ll rez = 0 ;
    ll ans = 0 ;
    ll Min = inf ;
    while( 1 ) {
        for(ll i = 0; i < n; i++ ) {
            Min = min( 2 * abs( i - x ) + abs(a[i] - Y) , Min ) ;
        }
        if( x == X ) {
            rez += Min ;
            ans = rez ;
            break ;
        }
        x ++ ; rez ++ ;
        if( a[x] < y ) y = a[x] ;
    }
    x = p , y = q ;
    swap(x,X) ; swap(y,Y) ;
    rez = 0 ;
    while( 1 ) {
       for(ll i = 0; i < n; i++ ) {
            Min = min( 2 * abs( i - x ) + abs(a[i] - Y) , Min ) ;
        }
        if( x == X ) {
            rez += Min ;
            cout << max( ans, rez ) << endl ;
            exit(0) ;
        }
        x -- ; rez ++ ;
        if( a[x] < y ) y = a[x] ;
    }
    return 0 ;
}


#pragma comment(linker,"/STACK:100000000,100000000")

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <stack>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <iomanip>
#include <queue>
#include <map>
#include <functional>
#include <memory.h>
#include <list>
#include <sstream>
#include <ctime>
#include <climits>

using namespace std;

/* Constants begin */
const long long inf = 1e18+7;
const long long mod = 1e9+7;
const double eps = 1e-10;
const double PI = 2*acos(0.0);
const double E = 2.71828;
/* Constants end */

/* Defines begin */
#define pb push_back
#define mp make_pair
#define ll long long
#define ld long double
#define F first
#define S second
#define all(a) (a).begin(), a.end()
#define forn(i,n) for (ll (i)=0;(i)<(ll)(n);(i)++)
#define ford(i,n) for (ll (i)=(n)-1;(i)>=(ll)0;(i)--)
#define foreach(itr,c) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
/* Defines end */

ll a[100005] ;

int main(void) {
 //   #ifndef ONLINE_JUDGE
        freopen("input.txt","rt",stdin);
        freopen("output.txt","wt",stdout);
 //   #endif
    ll n ; cin >> n ;
    forn(i,n) cin >> a[i] ;
    ll x, y, X, Y;
    cin >> x >> y >> X >> Y ;
    if( x > X ) swap(x,X) , swap(y,Y) ;
    x -- ; X -- ; y -- ; Y -- ;
    ll p = x , q = y ;
    ll rez = 0 ;
    ll ans = 0 ;
    ll Min = inf ;
    for(ll i = 0; i < n; i++ ) {
        Min = min( abs( i - x ) + abs( X - i ) + abs(a[i] - Y) , Min ) ;
    }
    cout << Min << endl ;
    return 0 ;
}


#pragma comment(linker,"/STACK:100000000,100000000")

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <stack>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <iomanip>
#include <queue>
#include <map>
#include <functional>
#include <memory.h>
#include <list>
#include <sstream>
#include <ctime>
#include <climits>

using namespace std;

/* Constants begin */
const long long inf = 1e18+7;
const long long mod = 1e9+7;
const double eps = 1e-10;
const double PI = 2*acos(0.0);
const double E = 2.71828;
/* Constants end */

/* Defines begin */
#define pb push_back
#define mp make_pair
#define ll long long
#define ld long double
#define F first
#define S second
#define all(a) (a).begin(), a.end()
#define forn(i,n) for (ll (i)=0;(i)<(ll)(n);(i)++)
#define ford(i,n) for (ll (i)=(n)-1;(i)>=(ll)0;(i)--)
#define foreach(itr,c) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
/* Defines end */

ll a[100005] ;
ll qx[100005], qy[100005] ;
ll dx[]={0,1,0,-1};
ll dy[]={1,0,-1,0};
ll used[105][100005], dist[105][100005] ;

int main(void) {
 //   #ifndef ONLINE_JUDGE
        freopen("input.txt","rt",stdin);
        freopen("output.txt","wt",stdout);
 //   #endif
    ll n ; cin >> n;
    forn(i,n) cin >> a[i] ;
    ll x, y, lx, ly ;
        cin >> x >> y >> lx >> ly ;
    x --; y --; lx --; ly --;
    ll qt, qh ; qt = qh = 0 ;
    qx[qt] = x; qy[qt] = y; qt++ ;
    used[x][y] = 1;
    for(; qh < qt; qh++) {
        ll nx = qx[qh], ny = qy[qh] ;
        forn(dir,4) {
            x = nx + dx[dir];
            y = ny + dy[dir] ;
            if( x > n || x < 0 ) continue;
            if( y < 0 ) continue;
            y = min(y,a[x]) ;
                if( !used[x][y] ) {
                    dist[x][y] = dist[nx][ny] + 1;
                    used[x][y] = 1;
                    qx[qt] = x; qy[qt] = y; qt ++;
                }
        }
    }
    cout << dist[lx][ly] << endl ;
    return 0 ;
}


#pragma comment(linker,"/STACK:100000000,100000000")

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <stack>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <iomanip>
#include <queue>
#include <map>
#include <functional>
#include <memory.h>
#include <list>
#include <sstream>
#include <ctime>
#include <climits>

using namespace std;

/* Constants begin */
const long long inf = 1e18+7;
const long long mod = 1e9+7;
const double eps = 1e-10;
const double PI = 2*acos(0.0);
const double E = 2.71828;
/* Constants end */

/* Defines begin */
#define pb push_back
#define mp make_pair
#define ll long long
#define ld long double
#define F first
#define S second
#define all(a) (a).begin(), a.end()
#define forn(i,n) for (ll (i)=0;(i)<(ll)(n);(i)++)
#define ford(i,n) for (ll (i)=(n)-1;(i)>=(ll)0;(i)--)
#define foreach(itr,c) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
/* Defines end */

ll a[100005] ;

int main(void) {
 //   #ifndef ONLINE_JUDGE
        freopen("input.txt","rt",stdin);
        freopen("output.txt","wt",stdout);
 //   #endif
    ll n ; cin >> n ;
    forn(i,n) cin >> a[i] ;
    ll x, y, X, Y;
    cin >> x >> y >> X >> Y ;
    if( x > X ) swap(x,X) , swap(y,Y) ;
    x -- ; X -- ; y -- ; Y -- ;
    ll Min = inf ;
    for(ll i = 0; i < n; i++ ) {
        Min = min( abs( i - x ) + abs( X - i ) + abs( min(a[i],y) - Y) , Min ) ;
    }
    cout << Min << endl ;
    return 0 ;
}


#pragma comment(linker,"/STACK:100000000,100000000")

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <stack>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <iomanip>
#include <queue>
#include <map>
#include <functional>
#include <memory.h>
#include <list>
#include <sstream>
#include <ctime>
#include <climits>

using namespace std;

/* Constants begin */
const long long inf = 1e18+7;
const long long mod = 1e9+7;
const double eps = 1e-10;
const double PI = 2*acos(0.0);
const double E = 2.71828;
/* Constants end */

/* Defines begin */
#define pb push_back
#define mp make_pair
#define ll long long
#define ld long double
#define F first
#define S second
#define all(a) (a).begin(), a.end()
#define forn(i,n) for (ll (i)=0;(i)<(ll)(n);(i)++)
#define ford(i,n) for (ll (i)=(n)-1;(i)>=(ll)0;(i)--)
#define foreach(itr,c) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
/* Defines end */

ll a[100005] ;
ll qx[100005], qy[100005] ;
ll dx[]={0,1,0,-1};
ll dy[]={1,0,-1,0};
ll used[1005][1005], dist[1005][1005] ;

int main(void) {
 //   #ifndef ONLINE_JUDGE
        freopen("input.txt","rt",stdin);
        freopen("output.txt","wt",stdout);
 //   #endif
    ll n ; cin >> n;
    forn(i,n) cin >> a[i] ;
    ll x, y, lx, ly ;
        cin >> x >> y >> lx >> ly ;
    x --; y --; lx --; ly --;
    ll qt, qh ; qt = qh = 0 ;
    qx[qt] = x; qy[qt] = y; qt++ ;
    for(; qh < qt; qh++) {
        ll nx = qx[qh], ny = qy[qh] ;
        forn(dir,4) {
            x = nx + dx[dir];
            y = ny + dy[dir] ;
            if( x > n || x < 0 ) continue;
            if( y < 0 ) continue;
            y = min(y,a[x]) ;
                if( !used[x][y] ) {
                    dist[x][y] = dist[nx][ny] + 1;
                    used[x][y] = 1;
                    qx[qt] = x; qy[qt] = y; qt ++;
                }
        }
    }
    cout << dist[lx][ly] << endl ;
    return 0 ;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include<utility>
#include<queue>
#include<cstring>
using namespace std;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
int a[105],n,c1,c2,r1,r2,d[111][100100];
int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {cin>>a[i];a[i]++;}
    cin>>r1>>c1>>r2>>c2;
    memset(d,-1,sizeof(d));
    d[r1][c1]=0;
    queue< pair<int,int> >q;
    q.push(make_pair(r1,c1));
    while(!q.empty() && d[r2][c2]<0 )
    {
        pair<int,int>u=q.front();q.pop();
        int x=u.first,y=u.second;
        for(int i=0;i<4;i++)
        {
            int xx=x+dx[i];
            int yy=min(a[xx],y+dy[i]);
            if(xx>0 && yy>0 && xx<=n && yy<=a[xx] && d[xx][yy]<0)
            {d[xx][yy]=d[x][y]+1;
            q.push(make_pair(xx,yy));}
        }
    }
    cout<<d[r2][c2];
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include<utility>
#include<queue>
#include<cstring>
using namespace std;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
int a[105],n,c1,c2,r1,r2,d[111][100100];
struct node
{
    int p,q;
}w[10000005];
int front,rear;
int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {cin>>a[i];a[i]++;}
    cin>>r1>>c1>>r2>>c2;
    memset(d,-1,sizeof(d));
    d[r1][c1]=0;
    node tmp;
    tmp.p=r1;
    tmp.q=c1;
    w[rear]=tmp;
    rear++;
    while(front<rear && d[r2][c2]<0 )
    {
        node u=w[front];front++;
        int x=u.p,y=u.q;
        for(int i=0;i<4;i++)
        {
            int xx=x+dx[i];
            int yy=min(a[xx],y+dy[i]);
            if(xx>0 && yy>0 && xx<=n && yy<=a[xx] && d[xx][yy]<0)
            {d[xx][yy]=d[x][y]+1;
            tmp.p=xx;
            tmp.q=yy;
            w[rear++]=tmp;   }
        }
    }
    cout<<d[r2][c2];
    //system("pause");
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include<utility>
#include<queue>
#include<cstring>
using namespace std;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
int a[105],n,c1,c2,r1,r2,d[111][100100];
int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {cin>>a[i];a[i]++;}
    cin>>r1>>c1>>r2>>c2;
    memset(d,-1,sizeof(d));
    d[r1][c1]=0;
    queue< pair<int,int> >q;
    q.push(make_pair(r1,c1));
    while(!q.empty() && d[r2][c2]<0 )
    {
        pair<int,int>u=q.front();q.pop();
        int x=u.first,y=u.second;
        for(int i=0;i<4;i++)
        {
            int xx=x+dx[i];
            int yy=min(a[xx],y+dy[i]);
            if(xx>0 && yy>0 && xx<=n && yy<=a[xx] && d[xx][yy]<0)
            d[xx][yy]=d[x][y]+1;
            q.push(make_pair(xx,yy));
        }
    }
    cout<<d[r2][c2];
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include<utility>
#include<queue>
#include<cstring>
using namespace std;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
int a[105],n,c1,c2,r1,r2,d[111][100100];
struct node
{
    int p,q;
}w[10005];
int front,rear;
int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {cin>>a[i];a[i]++;}
    cin>>r1>>c1>>r2>>c2;
    memset(d,-1,sizeof(d));
    d[r1][c1]=0;
    node tmp;
    tmp.p=r1;
    tmp.q=c1;
    w[rear]=tmp;
    rear++;
    while(front<rear && d[r2][c2]<0 )
    {
        node u=w[front];front++;
        int x=u.p,y=u.q;
        for(int i=0;i<4;i++)
        {
            int xx=x+dx[i];
            int yy=min(a[xx],y+dy[i]);
            if(xx>0 && yy>0 && xx<=n && yy<=a[xx] && d[xx][yy]<0)
            {d[xx][yy]=d[x][y]+1;
            tmp.p=xx;
            tmp.q=yy;
            w[rear++]=tmp;   }
        }
    }
    cout<<d[r2][c2];
    //system("pause");
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include<utility>
#include<queue>
#include<cstring>
using namespace std;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
int a[105],n,c1,c2,r1,r2,d[111][100100];
struct node
{
    int p,q;
}w[1000005];
int front,rear;
int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {cin>>a[i];a[i]++;}
    cin>>r1>>c1>>r2>>c2;
    memset(d,-1,sizeof(d));
    d[r1][c1]=0;
    node tmp;
    tmp.p=r1;
    tmp.q=c1;
    w[rear]=tmp;
    rear++;
    while(front<rear && d[r2][c2]<0 )
    {
        node u=w[front];front++;
        int x=u.p,y=u.q;
        for(int i=0;i<4;i++)
        {
            int xx=x+dx[i];
            int yy=min(a[xx],y+dy[i]);
            if(xx>0 && yy>0 && xx<=n && yy<=a[xx] && d[xx][yy]<0)
            {d[xx][yy]=d[x][y]+1;
            tmp.p=xx;
            tmp.q=yy;
            w[rear++]=tmp;   }
        }
    }
    cout<<d[r2][c2];
    //system("pause");
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <string>
using namespace std;
int r1,c1,r2,c2,n,a[111],ans=11111111;
int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    cin>>n;
    for (int i=1;i<=n;i++)
        cin>>a[i];
    cin>>r1>>c1>>r2>>c2;
    for (int i=1;i<=n;i++){
        int k=abs(i-r1);
        int cur=c1;
        for (int j=min(r1,i);j<=max(r1,i);j++)
            cur=min(cur,a[j]+1);
        for (int j=min(r2,i);j<=max(r2,i);j++)
            cur=min(cur,a[j]+1);
        k+=abs(i-r2)+abs(cur-c2);
        if (k<ans)ans=k;
    }
    cout<<ans;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include<utility>
#include<queue>
#include<cstring>
using namespace std;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
int a[105],n,c1,c2,r1,r2,d[111][100100];
struct node
{
    int p,q;
}w[10005];
int front,rear;
int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {cin>>a[i];a[i]++;}
    cin>>r1>>c1>>r2>>c2;
    memset(d,-1,sizeof(d));
    d[r1][c1]=0;
    node tmp;
    tmp.p=r1;
    tmp.q=c1;
    w[rear]=tmp;
    rear++;
    while(front<rear && d[r2][c2]<0 )
    {
        node u=w[front];front++;
        int x=u.p,y=u.q;
        for(int i=0;i<4;i++)
        {
            int xx=x+dx[i];
            int yy=min(a[xx],y+dy[i]);
            if(xx>0 && yy>0 && xx<=n && yy<=a[xx] && d[xx][yy]<0)
            {d[xx][yy]=d[x][y]+1;
            tmp.p=xx;
            tmp.q=yy;
            w[rear++]=tmp;   }
        }
    }
    cout<<d[r2][c2];
    //system("pause");
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include<utility>
#include<queue>
#include<cstring>
using namespace std;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
int a[105],n,c1,c2,r1,r2,d[111][100100];
struct node
{
    int p,q;
}w[100000005];
int front,rear;
int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {cin>>a[i];a[i]++;}
    cin>>r1>>c1>>r2>>c2;
    memset(d,-1,sizeof(d));
    d[r1][c1]=0;
    node tmp;
    tmp.p=r1;
    tmp.q=c1;
    w[rear]=tmp;
    rear++;
    while(front<rear && d[r2][c2]<0 )
    {
        node u=w[front];front++;
        int x=u.p,y=u.q;
        for(int i=0;i<4;i++)
        {
            int xx=x+dx[i];
            int yy=min(a[xx],y+dy[i]);
            if(xx>0 && yy>0 && xx<=n && yy<=a[xx] && d[xx][yy]<0)
            {d[xx][yy]=d[x][y]+1;
            tmp.p=xx;
            tmp.q=yy;
            w[rear++]=tmp;   }
        }
    }
    cout<<d[r2][c2];
    //system("pause");
    return 0;
}


//Pham Huu Canh
//C. Text Editor
//Algorithm:
//Complexity:
//AC:

#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>

#define max64 9223372036854775807LL
#define max32 2147483647
#define maxty 1001001001
#define max16 32767
#define EPS 1e-8
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define PQ priority_queue
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define timmax(x, y)    ((x) > (y) ? (x) : (y))
#define timmin(x, y)    ((x) < (y) ? (x) : (y))
#define fori(i, n)      for((i) = 0; (i) < (n); (i)++)
#define ford(i, n)      for((i) = (n-1); (i) >= 0; (i)--)
#define fore(i, v)      for(typeof(v.begin()) i = v.begin(); i != v.end(); i++)
#define repi(i, a, b)   for((i) = (a); (i) <= (b); (i)++)
#define repd(i, a, b)   for((i) = (a); (i) >= (b); (i)--)
#define all(tmpv)      tmpv.begin(), tmpv.end()

#define fii "input.txt"
#define foo "output.txt"
#define MOD 1000000007
#define inf 1000111000111000111LL

using namespace std;

typedef pair<int, int> II;
typedef vector<int> VI;

int a[105];

void input()
{
    int i, r1, r2, c1, c2, res, n;

    scanf("%d", &n);
    repi(i, 1, n)   scanf("%d", &a[i]), a[i]++;

    scanf("%d %d %d %d", &r1, &c1, &r2, &c2);

    res = 0;
    while (r1 < r2){
        res++;
        r1++;
        c1 = timmin(c1, a[r1]);
    }

    while (r1 > r2){
        res++;
        r1--;
        c1 = timmin(c1, a[r1]);
    }
    res += abs(c1 - c2);

    printf("%d", res);
}

int main()
{
   // #ifndef ONLINE_JUDGE
        freopen(fii,"r",stdin);
        freopen(foo,"w",stdout);
   // #endif

    input();

    return 0;
}


//Pham Huu Canh
//C. Text Editor
//Algorithm: BFS
//Complexity: O(n * nmax)
//AC: 154ms

#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>

#define max64 9223372036854775807LL
#define max32 2147483647
#define maxty 1001001001
#define max16 32767
#define EPS 1e-8
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define PQ priority_queue
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define timmax(x, y)    ((x) > (y) ? (x) : (y))
#define timmin(x, y)    ((x) < (y) ? (x) : (y))
#define fori(i, n)      for((i) = 0; (i) < (n); (i)++)
#define ford(i, n)      for((i) = (n-1); (i) >= 0; (i)--)
#define fore(i, v)      for(typeof(v.begin()) i = v.begin(); i != v.end(); i++)
#define repi(i, a, b)   for((i) = (a); (i) <= (b); (i)++)
#define repd(i, a, b)   for((i) = (a); (i) >= (b); (i)--)
#define all(tmpv)      tmpv.begin(), tmpv.end()

#define fii "input.txt"
#define foo "output.txt"
#define MOD 1000000007
#define inf 1000111000111000111LL

using namespace std;

typedef pair<int, int> II;
typedef vector<int> VI;

struct td{
    int x, y;
}H[4] = {-1, 0, 0, 1, 1, 0, 0, -1};
int n;
int d[105][100005], a[105];

int bfs(int stx, int sty, int enx, int eny){
    if (stx == enx && sty == eny)   return 0;

    II p, t;
    queue<II> q;
    int h;

    memset(d, -1, sizeof(d));
    d[stx][sty] = 0;
    q.push(mp(stx, sty));

    while (!q.empty()){
        p = q.front(), q.pop();

        fori(h, 4){
            t = mp(p.fi + H[h].x, p.se + H[h].y);
            if (t.fi < 1 || t.fi > n || t.se < 1)   continue;
            if (t.se > a[t.fi]) t.se = a[t.fi];
            if (d[t.fi][t.se] == -1){
                d[t.fi][t.se] = d[p.fi][p.se] + 1;
                if (t.fi == enx && t.se == eny) return d[enx][eny];
                q.push(t);
            }
        }
    }
    return 0;
}

void input()
{
    int i, r1, c1, r2, c2;

    scanf("%d", &n);
    repi(i, 1, n)   scanf("%d", &a[i]), a[i]++;

    scanf("%d %d %d %d", &r1, &c1, &r2, &c2);
    printf("%d\n", bfs(r1, c1, r2, c2));
}

int main()
{
    #ifndef ONLINE_JUDGE
        freopen(fii,"r",stdin);
        freopen(foo,"w",stdout);
    #endif

    input();

    return 0;
}


//Pham Huu Canh
//C. Text Editor
//Algorithm: BFS
//Complexity: O(n * nmax)
//AC: 154ms

#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>

#define max64 9223372036854775807LL
#define max32 2147483647
#define maxty 1001001001
#define max16 32767
#define EPS 1e-8
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define PQ priority_queue
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define timmax(x, y)    ((x) > (y) ? (x) : (y))
#define timmin(x, y)    ((x) < (y) ? (x) : (y))
#define fori(i, n)      for((i) = 0; (i) < (n); (i)++)
#define ford(i, n)      for((i) = (n-1); (i) >= 0; (i)--)
#define fore(i, v)      for(typeof(v.begin()) i = v.begin(); i != v.end(); i++)
#define repi(i, a, b)   for((i) = (a); (i) <= (b); (i)++)
#define repd(i, a, b)   for((i) = (a); (i) >= (b); (i)--)
#define all(tmpv)      tmpv.begin(), tmpv.end()

#define fii "input.txt"
#define foo "output.txt"
#define MOD 1000000007
#define inf 1000111000111000111LL

using namespace std;

typedef pair<int, int> II;
typedef vector<int> VI;

struct td{
    int x, y;
}H[4] = {-1, 0, 0, 1, 1, 0, 0, -1};
int n;
int d[105][100005], a[105];

int bfs(int stx, int sty, int enx, int eny){
    if (stx == enx && sty == eny)   return 0;

    II p, t;
    queue<II> q;
    int h;

    memset(d, -1, sizeof(d));
    d[stx][sty] = 0;
    q.push(mp(stx, sty));

    while (!q.empty()){
        p = q.front(), q.pop();

        fori(h, 4){
            t = mp(p.fi + H[h].x, p.se + H[h].y);
            if (t.fi < 1 || t.fi > n || t.se < 1)   continue;
            if (t.se > a[t.fi]) t.se = a[t.fi];
            if (d[t.fi][t.se] == -1){
                d[t.fi][t.se] = d[p.fi][p.se] + 1;
                if (t.fi == enx && t.se == eny) return d[enx][eny];
                q.push(t);
            }
        }
    }
    return 0;
}

void input()
{
    int i, r1, c1, r2, c2;

    scanf("%d", &n);
    repi(i, 1, n)   scanf("%d", &a[i]), a[i]++;

    scanf("%d %d %d %d", &r1, &c1, &r2, &c2);
    printf("%d\n", bfs(r1, c1, r2, c2));
}

int main()
{
    //#ifndef ONLINE_JUDGE
        freopen(fii,"r",stdin);
        freopen(foo,"w",stdout);
    //#endif

    input();

    return 0;
}


//Pham Huu Canh
//C. Text Editor
//Algorithm: BFS
//Complexity: O(n * nmax)
//AC: 154ms

#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>

#define max64 9223372036854775807LL
#define max32 2147483647
#define maxty 1001001001
#define max16 32767
#define EPS 1e-8
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define PQ priority_queue
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define timmax(x, y)    ((x) > (y) ? (x) : (y))
#define timmin(x, y)    ((x) < (y) ? (x) : (y))
#define fori(i, n)      for((i) = 0; (i) < (n); (i)++)
#define ford(i, n)      for((i) = (n-1); (i) >= 0; (i)--)
#define fore(i, v)      for(typeof(v.begin()) i = v.begin(); i != v.end(); i++)
#define repi(i, a, b)   for((i) = (a); (i) <= (b); (i)++)
#define repd(i, a, b)   for((i) = (a); (i) >= (b); (i)--)
#define all(tmpv)      tmpv.begin(), tmpv.end()

#define fii "input.txt"
#define foo "output.txt"
#define MOD 1000000007
#define inf 1000111000111000111LL

using namespace std;

typedef pair<int, int> II;
typedef vector<int> VI;

struct td{
    int x, y;
}H[4] = {-1, 0, 0, 1, 1, 0, 0 -1};
int n;
int d[105][100005], a[105];

int bfs(int stx, int sty, int enx, int eny){
    if (stx == sty && enx == eny)   return 0;

    II p, t;
    queue<II> q;
    int h;

    memset(d, -1, sizeof(d));
    d[stx][sty] = 0;
    q.push(mp(stx, sty));

    while (!q.empty()){
        p = q.front(), q.pop();

        fori(h, 4){
            t = mp(p.fi + H[h].x, p.se + H[h].y);
            if (t.fi < 1 || t.fi > n || t.se < 1)   continue;
            if (t.se > a[t.fi]) t.se = a[t.fi];
            if (d[t.fi][t.se] == -1){
                d[t.fi][t.se] = d[p.fi][p.se] + 1;
                if (t.fi == enx && t.se == eny) return d[enx][eny];
                q.push(t);
            }
        }
    }
    return 0;
}

void input()
{
    int i, r1, c1, r2, c2;

    scanf("%d", &n);
    repi(i, 1, n)   scanf("%d", &a[i]), a[i]++;

    scanf("%d %d %d %d", &r1, &c1, &r2, &c2);
    printf("%d\n", bfs(r1, c1, r2, c2));
}

int main()
{
    //#ifndef ONLINE_JUDGE
        freopen(fii,"r",stdin);
        freopen(foo,"w",stdout);
    //#endif

    input();

    return 0;
}


//Pham Huu Canh
//C. Text Editor
//Algorithm:
//Complexity:
//AC:

#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>

#define max64 9223372036854775807LL
#define max32 2147483647
#define maxty 1001001001
#define max16 32767
#define EPS 1e-8
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define PQ priority_queue
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define timmax(x, y)    ((x) > (y) ? (x) : (y))
#define timmin(x, y)    ((x) < (y) ? (x) : (y))
#define fori(i, n)      for((i) = 0; (i) < (n); (i)++)
#define ford(i, n)      for((i) = (n-1); (i) >= 0; (i)--)
#define fore(i, v)      for(typeof(v.begin()) i = v.begin(); i != v.end(); i++)
#define repi(i, a, b)   for((i) = (a); (i) <= (b); (i)++)
#define repd(i, a, b)   for((i) = (a); (i) >= (b); (i)--)
#define all(tmpv)      tmpv.begin(), tmpv.end()

#define fii "input.txt"
#define foo "output.txt"
#define MOD 1000000007
#define inf 1000111000111000111LL

using namespace std;

typedef pair<int, int> II;
typedef vector<int> VI;

int n;
int d[105][100005], a[105];

int bfs(int stx, int sty, int enx, int eny){
    II p, t;
    queue<II> q;

    memset(d, -1, sizeof(d));
    d[stx][sty] = 0;
    q.push(mp(stx, sty));

    while (!q.empty()){
        p = q.front(), q.pop();
        if (p.fi == enx && p.se == eny) return d[enx][eny];
        if (p.fi > 1){          //UP
            t = mp(p.fi - 1, timmin(a[p.fi - 1], p.se));
            if (d[t.fi][t.se] == -1){
                d[t.fi][t.se] = d[p.fi][p.se] + 1;
                q.push(t);
            }
        }
        if (p.fi < n){          //Down
            t = mp(p.fi + 1, timmin(a[p.fi + 1], p.se));
            if (d[t.fi][t.se] == -1){
                d[t.fi][t.se] = d[p.fi][p.se] + 1;
                q.push(t);
            }
        }
        if (p.se > 1){          //Left
            t = mp(p.fi, p.se - 1);
            if (d[t.fi][t.se] == -1){
                d[t.fi][t.se] = d[p.fi][p.se] + 1;
                q.push(t);
            }
        }
        if (p.se < a[p.fi]){    //Right
            t = mp(p.fi, p.se + 1);
            if (d[t.fi][t.se] == -1){
                d[t.fi][t.se] = d[p.fi][p.se] + 1;
                q.push(t);
            }
        }
    }
    return 0;
}

void input()
{
    int i, r1, c1, r2, c2;

    scanf("%d", &n);
    repi(i, 1, n)   scanf("%d", &a[i]), a[i]++;

    scanf("%d %d %d %d", &r1, &c1, &r2, &c2);
    printf("%d\n", bfs(r1, c1, r2, c2));
}

int main()
{
    //#ifndef ONLINE_JUDGE
        freopen(fii,"r",stdin);
        freopen(foo,"w",stdout);
    //#endif

    input();

    return 0;
}


//Pham Huu Canh
//C. Text Editor
//Algorithm:
//Complexity:
//AC:

#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>

#define max64 9223372036854775807LL
#define max32 2147483647
#define maxty 1001001001
#define max16 32767
#define EPS 1e-8
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define PQ priority_queue
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define timmax(x, y)    ((x) > (y) ? (x) : (y))
#define timmin(x, y)    ((x) < (y) ? (x) : (y))
#define fori(i, n)      for((i) = 0; (i) < (n); (i)++)
#define ford(i, n)      for((i) = (n-1); (i) >= 0; (i)--)
#define fore(i, v)      for(typeof(v.begin()) i = v.begin(); i != v.end(); i++)
#define repi(i, a, b)   for((i) = (a); (i) <= (b); (i)++)
#define repd(i, a, b)   for((i) = (a); (i) >= (b); (i)--)
#define all(tmpv)      tmpv.begin(), tmpv.end()

#define fii "input.txt"
#define foo "output.txt"
#define MOD 1000000007
#define inf 1000111000111000111LL

using namespace std;

typedef pair<int, int> II;
typedef vector<int> VI;

int a[105];

void input()
{
    int i, n, val, res;
    int c, c1, c2, r, r1, r2;

    scanf("%d", &n);
    repi(i, 1, n)   scanf("%d", &a[i]), a[i]++;

    scanf("%d %d %d %d", &r1, &c1, &r2, &c2);
    res = maxty;
    repi(i, 1, n){
        //move from row r1 to row i
        val = 0;
        r = r1, c = c1;
        while (r < i){
            val++, r++;
            c = timmin(c, a[r]);
        }
        while (r > i){
            val++, r--;
            c = timmin(c, a[r]);
        }

        //move from row i to row r2
        while (r < r2){
            val++, r++;
            c = timmin(c, a[r]);
        }
        while (r > r2){
            val++, r--;
            c = timmin(c, a[r]);
        }
        val += abs(c - c2);

        ////
        res = timmin(res, val);
    }

    printf("%d", res);
}

int main()
{
    //#ifndef ONLINE_JUDGE
        freopen(fii,"r",stdin);
        freopen(foo,"w",stdout);
    //#endif

    input();

    return 0;
}


//Pham Huu Canh
//C. Text Editor
//Algorithm:
//Complexity:
//AC:

#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>

#define max64 9223372036854775807LL
#define max32 2147483647
#define maxty 1001001001
#define max16 32767
#define EPS 1e-8
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define PQ priority_queue
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define timmax(x, y)    ((x) > (y) ? (x) : (y))
#define timmin(x, y)    ((x) < (y) ? (x) : (y))
#define fori(i, n)      for((i) = 0; (i) < (n); (i)++)
#define ford(i, n)      for((i) = (n-1); (i) >= 0; (i)--)
#define fore(i, v)      for(typeof(v.begin()) i = v.begin(); i != v.end(); i++)
#define repi(i, a, b)   for((i) = (a); (i) <= (b); (i)++)
#define repd(i, a, b)   for((i) = (a); (i) >= (b); (i)--)
#define all(tmpv)      tmpv.begin(), tmpv.end()

#define fii "input.txt"
#define foo "output.txt"
#define MOD 1000000007
#define inf 1000111000111000111LL

using namespace std;

typedef pair<int, int> II;
typedef vector<int> VI;

int a[105];

void input()
{
    int i, r1, r2, c1, c2, res, n;

    scanf("%d", &n);
    repi(i, 1, n)   scanf("%d", &a[i]), a[i]++;

    scanf("%d %d %d %d", &r1, &c1, &r2, &c2);

    res = 0;
    while (r1 < r2){
        res++;
        r1++;
        c1 = timmin(c1, a[r1]);
    }

    while (r1 > r2){
        res++;
        r1--;
        c1 = timmin(c1, a[r1]);
    }
    res += abs(c1 - c2);

    printf("%d", res);
}

int main()
{
    #ifndef ONLINE_JUDGE
        freopen(fii,"r",stdin);
        freopen(foo,"w",stdout);
    #endif

    input();

    return 0;
}


#include<algorithm>
#include <iostream>
#include <cstdio>
#include<cmath>
using namespace std;

int a[1000];

int f(int r1,int r2, int c)
{
    int ret=c;
    if (r2>=r1){
        for(int j=r1; j<=r2;j++)
                ret = min(ret, a[j]+1);
//            if(a[j]+1<ret)
//                ret=a[j]+1;
    }
    else {
        for( int j=r1; j>=r2; j--)
            ret = min(ret,a[j]+1);
//            if(a[j] + 1< ret)
//                ret=a[j]+1;
    }

    return ret;
}

int main()
{
    int c1,c2,r1,r2;
    int n;
    int co1,co2;

    freopen("input.txt", "r", stdin);

    freopen("output.txt","w", stdout);
    cin>>n;
    for(int j=1; j<=n;j++)
        cin>>a[j];
    cin>>r1>>c1>>r2>>c2;
    int ans = 999999;

    for(int j=1;j<=n;j++){
        co1 = f(r1,j,c1);
        co2 = f(j, r2, co1);
//        if(ans>abs(r1-j) + abs(j-r2) +abs(co2-c2))
//            ans = abs(r1-j) + abs(j-r2) +abs(co2-c2);
        ans = min(ans, abs(r1-j) + abs(j-r2) +abs(co2-c2) );
    }

    cout<<ans<<endl;

    return 0;
}


#include<algorithm>
#include <iostream>
#include <cstdio>
#include<cmath>
using namespace std;

int a[1000];

int f(int r1,int r2, int c)
{
    int ret=c;
    if (r2>=r1){
        for(int j=r1; j<=r2;j++)
                ret = min(ret, a[j]+1);
//            if(a[j]+1<ret)
//                ret=a[j]+1;
    }
    else {
        for( int j=r1; j>=r2; j--)
            ret = min(ret,a[j]+1);
//            if(a[j] + 1< ret)
//                ret=a[j]+1;
    }

    return ret;
}

int main()
{
    int c1,c2,r1,r2;
    int n;
    int co1,co2;
//
//    freopen("input.txt", "r", stdin);
//
//    freopen("output.txt","w", stdout);
    cin>>n;
    for(int j=1; j<=n;j++)
        cin>>a[j];
    cin>>r1>>c1>>r2>>c2;
    int ans = 999999;

    for(int j=1;j<=n;j++){
        co1 = f(r1,j,c1);
        co2 = f(j, r2, co1);
//        if(ans>abs(r1-j) + abs(j-r2) +abs(co2-c2))
//            ans = abs(r1-j) + abs(j-r2) +abs(co2-c2);
        ans = min(ans, abs(r1-j) + abs(j-r2) +abs(co2-c2) );
    }

    cout<<ans<<endl;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <stack>
#include <map>
#include <iomanip>
#define PI acos(-1.0)
#define Max 2005
#define inf 1<<28
#define LL(x) (x<<1)
#define RR(x) (x<<1|1)
#define FOR(i,s,t) for(int i=(s);  i<=(t);++i)
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
int a[100000];
bool vis[200][100005];
int sx, sy, ex, ey;
int mx[4] = {0, 0, -1, 1};
int my[4] = {1, -1, 0, 0};
struct kdq {
    int x, y;
    int num;
};
int n;
int bfs()
{
    vis[sx][sy] = 1;
    queue<kdq>
    q;
    kdq x;
    x.x = sx;
    x.y = sy;
    x.num = 0;
    q.push ( x );

    while ( !q.empty() ) {
        kdq temp = q.front();
        q.pop();

        if ( temp.x == ex && temp.y == ey ) {
            return temp.num;
        };

        for ( int i = 0;
                i < 4; i++ ) {
            int tx = temp.x + mx[i];
            int ty = temp.y + my[i];

            if ( tx == 0 ) {
                tx = 1;
            }

            elseif ( tx == n + 1 ) {
                tx = n;
            }

            if ( ty >    a[tx] + 1 ) {
                ty = a[tx] + 1;
            }

            elseif ( ty <= 0 ) {
                ty = 1;
            }

            if ( !vis[tx][ty] ) {
                vis[tx][ty] = 1;
                kdq dd;
                dd.x = tx;
                dd.y = ty;
                dd.num = temp.num + 1;
                q.push ( dd );
            }
        }
    }

    return 0;
}
void solve()
{
    freopen ( "input.txt", "r", stdin );
    freopen ( "output.txt", "w", stdout );

    while ( cin >> n ) {
        for ( int i = 1; i <= n; i++ ) {
            scanf ( "%d", &a[i] );
        }

        cin >> sx >> sy >> ex >> ey;
        cout << bfs() << endl;
        memset ( vis, 0, sizeof ( vis ) );
    }
}
int main()
{
    solve();
    return 0;
}


#include <iostream>
#include <cstdio>
#include<cmath>
using namespace std;

int a[1000];

int f(int r1,int r2, int c)
{
    int ret=c;
    if (r2>=r1){
        for(int j=r1; j<=r2;j++)
            if(a[j]+1<ret)
                ret=a[j]+1;
    }
    else {
        for( int j=r1; j>=r2; j--)
            if(a[j] + 1< ret)
                ret=a[j]+1;
    }

    return ret;
}

int main()
{
    int c1,c2,r1,r2;
    int n;
    int co1,co2;

    freopen("input.txt", "r", stdin);

    freopen("output.txt","w", stdout);
    cin>>n;
    for(int j=1; j<=n;j++)
        cin>>a[j];
    cin>>r1>>c1>>r2>>c2;
    int ans = 999999;
    for(int j=1;j<=n;j++){
        co1 = f(r1,j,c1);
        co2 = f(j, r2, co1);
        if(ans>abs(c1-co1)+abs(r1-j) + abs(j-r2) +abs(co2-c2))
            ans = abs(c1-co1)+abs(r1-j) + abs(j-r2) +abs(co2-c2);
    }

    cout<<ans<<endl;

    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#define REP(i,n) for(int i=0; i<(n); ++i)
#define FOR(i,s,t) for(int i=(s); i<(t); ++i)
using namespace std;
typedef long long int64;

const int MAXN = 1000005;
int n, m;
int arr[MAXN];

int getMin(int r1, int r2, int c){
    int minx = c;
    if(r1 < r2){
        for(int i=r1+1; i<=r2; ++i){
            if(arr[i]+1 < minx)
            minx=arr[i]+1;
        }
    }
    else{
        for(int i=r1-1; i>=r2; --i){
            if(arr[i]+1 < minx)
                minx=arr[i]+1;
        }
    }
    return minx;
}

int main(){
#ifdef LOCALfd
    freopen("input.txt","r", stdin);
    freopen("output.txt","w",stdout);
#endif
    int r1, c1, r2, c2;
    while(~scanf("%d", &n)){
        FOR(i,1,n+1) scanf("%d", &arr[i]);
        scanf("%d%d%d%d",&r1,&c1,&r2,&c2);
        int ans=MAXN+100;
        for(int i=1; i<=n; ++i){
            int min1 = getMin(r1, i, c1);
            int min2 = getMin(i, r2, min1);
            int tmp = abs(r1-i) + abs(i-r2) + abs(c2-min2);
            if(tmp < ans) ans=tmp;
        }
        printf("%d\n", ans);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <stack>
#include <map>
#include <iomanip>
#define PI acos(-1.0)
#define Max 2005
#define inf 1<<28
#define LL(x) (x<<1)
#define RR(x) (x<<1|1)
#define FOR(i,s,t) for(int i=(s);  i<=(t);++i)
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
int a[100000];
bool vis[200][100005];
int sx, sy, ex, ey;
int mx[4] = {0, 0, -1, 1};
int my[4] = {1, -1, 0, 0};
struct kdq {
    int x, y;
    int num;
};
int n;
int bfs()
{
    vis[sx][sy] = 1;
    queue<kdq>
    q;
    kdq x;
    x.x = sx;
    x.y = sy;
    x.num = 0;
    q.push ( x );

    while ( !q.empty() ) {
        kdq temp = q.front();
        q.pop();

        if ( temp.x == ex && temp.y == ey ) {
            return temp.num;
        };

        for ( int i = 0;
                i < 4; i++ ) {
            int tx = temp.x + mx[i];
            int ty = temp.y + my[i];

            if ( tx == 0 ) {
                tx = 1;
            }

            else if ( tx == n + 1 ) {
                tx = n;
            }

            if ( ty >    a[tx] + 1 ) {
                ty = a[tx] + 1;
            }

            else if ( ty <= 0 ) {
                ty = 1;
            }

            if ( !vis[tx][ty] ) {
                vis[tx][ty] = 1;
                kdq dd;
                dd.x = tx;
                dd.y = ty;
                dd.num = temp.num + 1;
                q.push ( dd );
            }
        }
    }

    return 0;
}
void solve()
{
    freopen ( "input.txt", "r", stdin );
    freopen ( "output.txt", "w", stdout );

    while ( cin >> n ) {
        for ( int i = 1; i <= n; i++ ) {
            scanf ( "%d", &a[i] );
        }

        cin >> sx >> sy >> ex >> ey;
        cout << bfs() << endl;
        memset ( vis, 0, sizeof ( vis ) );
    }
}
int main()
{
    solve();
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <stack>
#include <map>
#include <iomanip>
#define PI acos(-1.0)
#define Max 2005
#define inf 1<<28
#define LL(x) (x<<1)
#define RR(x) (x<<1|1)
#define FOR(i,s,t) for(int i=(s);  i<=(t);++i)
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
int a[100000];
bool vis[200][100005];
int sx, sy, ex, ey;
int mx[4] = {0, 0, -1, 1};
int my[4] = {1, -1, 0, 0};
struct kdq {
    int x, y;
    int num;
};
int n;
int bfs()
{
    vis[sx][sy] = 1;
    queue<kdq>
    q;
    kdq x;
    x.x = sx;
    x.y = sy;
    x.num = 0;
    q.push ( x );

    while ( !q.empty() ) {
        kdq temp = q.front();
        q.pop();

        if ( temp.x == ex && temp.y == ey ) {
            return temp.num;
        };

        for ( int i = 0;
                i < 4; i++ ) {
            int tx = temp.x + mx[i];
            int ty = temp.y + my[i];

            if ( tx == 0 ) {
                tx = 1;
            }

            else if ( tx == n + 1 ) {
                tx = n;
            }

            if ( ty >    a[tx] + 1 ) {
                ty = a[tx] + 1;
            }

            else if ( ty <= 0 ) {
                ty = 1;
            }

            if ( !vis[tx][ty] ) {
                vis[tx][ty] = 1;
                kdq dd;
                dd.x = tx;
                dd.y = ty;
                dd.num = temp.num + 1;
                q.push ( dd );
            }
        }
    }

    return 0;
}
void solve()
{
    freopen ( "input.txt", "r", stdin );
    freopen ( "output.txt", "w", stdout );

    while ( cin >> n ) {
        for ( int i = 1; i <= n; i++ ) {
            scanf ( "%d", &a[i] );
        }

        cin >> sx >> sy >> ex >> ey;
        cout << bfs() << endl;

    }
}
int main()
{
    solve();
    return 0;
}


#include <iostream>
 #include <cstdio>
 #include <algorithm>
 #include <string>
 #include <cmath>
 #include <cstring>
 #include <queue>
 #include <set>
 #include <vector>
 #include <stack>
 #include <map>
 #include <iomanip>
 #define PI acos(-1.0)
 #define Max 2005
 #define inf 1<<28
 #define LL(x) (x<<1)
 #define RR(x) (x<<1|1)
 #define FOR(i,s,t) for(int i=(s);  i<=(t);++i)
 #define ll long long
 #define mem(a,b) memset(a,b,sizeof(a))
 using namespace std;
  int a[100000];
 bool vis[200][100005];
 int sx,sy,ex,ey;
 int mx[4]= {0,0,-1,1};
 int my[4]= {1,-1,0,0};
 struct kdq { int x,y;
 int num;
 };
 int n;
 int bfs() { vis[sx][sy]=1;
 queue<kdq>
q;
 kdq x;
 x.x=sx;
 x.y=sy;
 x.num=0;
 q.push(x);
 while(!q.empty()) { kdq temp=q.front();
 q.pop();
 if(temp.x==ex&&temp.y==ey) { return temp.num;
 };
 for(int i=0;
 i<4;
 i++) { int tx=temp.x+mx[i];
 int ty=temp.y+my[i];
 if(tx==0) tx=1;
 else if(tx==n+1) tx=n;
 if(ty>
a[tx]+1) ty=a[tx]+1;
 else if(ty<=0) ty=1;
 if(!vis[tx][ty]) { vis[tx][ty]=1;
 kdq dd;
 dd.x=tx;
 dd.y=ty;
 dd.num=temp.num+1;
 q.push(dd);
 } } } return 0;
 } void solve() { freopen("input.txt","r",stdin);
 freopen("output.txt","w",stdout);
 while(cin>>
n) {  for(int i=1;
 i<=n;
 i++) scanf("%d",&a[i]);
 cin>>
sx>>
sy>>
ex>>
ey;
 cout<<bfs()<<endl;
 memset(vis,0,sizeof(vis));
 } } int main() { solve();
 return 0;
 }


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#define REP(i,n) for(int i=0; i<(n); ++i)
#define FOR(i,s,t) for(int i=(s); i<(t); ++i)
using namespace std;
typedef long long int64;

const int MAXN = 1000005;
int n, m;
int arr[MAXN];

int getMin(int r1, int r2, int c){
    int minx = c;
    if(r1 < r2){
        for(int i=r1+1; i<=r2; ++i){
            if(arr[i]+1 < minx)
            minx=arr[i]+1;
        }
    }
    else{
        for(int i=r1-1; i>=r2; --i){
            if(arr[i]+1 < minx)
                minx=arr[i]+1;
        }
    }
    return minx;
}

int main(){
#ifndef LOCAL
    freopen("input.txt","r", stdin);
    freopen("output.txt","w",stdout);
#endif
    int r1, c1, r2, c2;
    while(~scanf("%d", &n)){
        FOR(i,1,n+1) scanf("%d", &arr[i]);
        scanf("%d%d%d%d",&r1,&c1,&r2,&c2);
        int ans=MAXN+100;
        for(int i=1; i<=n; ++i){
            int min1 = getMin(r1, i, c1);
            int min2 = getMin(i, r2, min1);
            int tmp = abs(r1-i) + abs(i-r2) + abs(c2-min2);
            if(tmp < ans) ans=tmp;
        }
        printf("%d\n", ans);
    }
    return 0;
}


#include<algorithm>
#include <iostream>
#include <cstdio>
#include<cmath>
using namespace std;

int a[1000];

int f(int r1,int r2, int c)
{
    int ret=c;
    if (r2>=r1){
        for(int j=r1; j<=r2;j++)
                ret = min(ret, a[j]+1);
//            if(a[j]+1<ret)
//                ret=a[j]+1;
    }
    else {
        for( int j=r1; j>=r2; j--)
            ret = min(ret,a[j]+1);
//            if(a[j] + 1< ret)
//                ret=a[j]+1;
    }

    return ret;
}

int main()
{
    int c1,c2,r1,r2;
    int n;
    int co1,co2;
//
    freopen("input.txt", "r", stdin);

    freopen("output.txt","w", stdout);
    cin>>n;
    for(int j=1; j<=n;j++)
        cin>>a[j];
    cin>>r1>>c1>>r2>>c2;
    int ans = 999999;

    for(int j=1;j<=n;j++){
        co1 = f(r1,j,c1);
        co2 = f(j, r2, co1);
//        if(ans>abs(r1-j) + abs(j-r2) +abs(co2-c2))
//            ans = abs(r1-j) + abs(j-r2) +abs(co2-c2);
        ans = min(ans, abs(r1-j) + abs(j-r2) +abs(co2-c2) );
    }

    cout<<ans<<endl;

    return 0;
}


#include <bits/stdc++.h>
#define REP(i,a,b) for(int i=a;i<b;i++)
using namespace std;

int len[200];

int main() {
    freopen("output.txt","w",stdout);
    freopen("input.txt","r",stdin);
    int n;
    cin >> n;
    bool debug = false;
    if (n == 100) debug = true;
    REP(i,1,n+1) cin >> len[i];
    int r1,r2,c1,c2;
    cin >> r1 >> c1 >> r2 >> c2;
    if (debug)  {
        cout << n << " ";
        REP(i,1,n+1) {
            cout << len[i] << " ";
            if (i%20 == 0) cout << endl;
        }
        cout << endl << r1 << " " << c1 << " " << r2 << " " << c2;
        debug = false;
    }

    int ans = 1e8;
    int dx = 1;
    if (r2 < r1) dx=-1;
    int pos = c1;
    for (int j = r1; j <= r2; j+=dx) {
        pos = min(pos,len[j]+1);
    }
    ans = min(ans, abs(c2-pos) + abs(r1-r2));
    if (ans== 2485 && debug){
        cout << 1 << " " << r1 << " " << c1 << " " << r2 << " " << c2 << " " << len[r1] << " " << len[r2] << endl;
    debug = false;}

    int pos2 = pos;
    for (int j = r2; j <= n && j >= 1; j+=dx) {
        pos2 = min(pos2,len[j]+1);
        ans = min(ans,2*abs(r2-j)+abs(r1-r2)+abs(c2-pos2));
    if (ans== 2485 && debug){
        cout << 1 << " " << r1 << " " << c1 << " " << r2 << " " << c2 << " " << len[r1] << " " << len[r2] << endl;
    debug = false;}
    }
    int pos3 = pos;
    dx*=-1;
    for (int j = r1; j <= n && j >= 1; j+=dx) {
        pos3 = min(pos3,len[j]+1);
        ans = min(ans,2*abs(r1-j)+abs(r1-r2)+abs(c2-pos3));
    if (ans== 2485 && debug){
        cout << 1 << " " << r1 << " " << c1 << " " << r2 << " " << c2 << " " << len[r1] << " " << len[r2] << endl;
    debug = false;}
    }
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>
#define REP(i,a,b) for(int i=a;i<b;i++)
using namespace std;

int len[200];

int main() {
    ios_base::sync_with_stdio(0);
    freopen("output.txt","w",stdout);
    freopen("input.txt","r",stdout);
    int n;
    cin >> n;
    REP(i,1,n+1) cin >> len[i];
    int r1,r2,c1,c2;
    cin >> r1 >> c1 >> r2 >> c2;
    int ans = 1e8;
    int dx = 1;
    if (r2 < r1) dx=-1;
    int pos = c1;
    for (int j = r1; j <= r2; j+=dx) {
        pos = min(pos,len[j]+1);
    }
    ans = min(ans, abs(c2-pos) + abs(r1-r2));
    int pos2 = pos;
    for (int j = r2; j <= n && j >= 1; j+=dx) {
        pos2 = min(pos2,len[j]+1);
        ans = min(ans,2*abs(r2-j)+abs(r1-r2)+abs(c2-pos2));
    }
    int pos3 = pos;
    dx*=-1;
    for (int j = r1; j <= n && j >= 1; j+=dx) {
        pos3 = min(pos3,len[j]+1);
        ans = min(ans,2*abs(r1-j)+abs(r1-r2)+abs(c2-pos3));
    }
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>
#define REP(i,a,b) for(int i=a;i<b;i++)
using namespace std;

int len[200];

int main() {
    freopen("output.txt","w",stdout);
    freopen("input.txt","r",stdin);
    int n;
    cin >> n;
    bool debug = false;
    if (n == 100) debug = true;
    REP(i,1,n+1) cin >> len[i];
    int r1,r2,c1,c2;
    cin >> r1 >> c1 >> r2 >> c2;
    int ans = 1e8;
    int dx = 1;
    if (r2 < r1) dx=-1;
    int pos = c1;
    for (int j = r1; j <= r2; j+=dx) {
        pos = min(pos,len[j]+1);
    }
    ans = min(ans, abs(c2-pos) + abs(r1-r2));
    if (ans== 2485 && debug){
        cout << 1 << " " << r1 << " " << c1 << " " << r2 << " " << c2 << " " << len[r1] << " " << len[r2] << endl;
    debug = false;}

    int pos2 = pos;
    for (int j = r2; j <= n && j >= 1; j+=dx) {
        pos2 = min(pos2,len[j]+1);
        ans = min(ans,2*abs(r2-j)+abs(r1-r2)+abs(c2-pos2));
    if (ans== 2485 && debug){
        cout << 1 << " " << r1 << " " << c1 << " " << r2 << " " << c2 << " " << len[r1] << " " << len[r2] << endl;
    debug = false;}
    }
    int pos3 = pos;
    dx*=-1;
    for (int j = r1; j <= n && j >= 1; j+=dx) {
        pos3 = min(pos3,len[j]+1);
        ans = min(ans,2*abs(r1-j)+abs(r1-r2)+abs(c2-pos3));
    if (ans== 2485 && debug){
        cout << 1 << " " << r1 << " " << c1 << " " << r2 << " " << c2 << " " << len[r1] << " " << len[r2] << endl;
    debug = false;}
    }
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>
#define REP(i,a,b) for(int i=a;i<b;i++)
using namespace std;

int len[200];

int main() {
    freopen("output.txt","w",stdout);
    freopen("input.txt","r",stdin);
    int n;
    cin >> n;
    REP(i,1,n+1) cin >> len[i];
    int r1,r2,c1,c2;
    cin >> r1 >> c1 >> r2 >> c2;
    int ans = 1e8;
    int dx = 1;
    if (r2 < r1) dx=-1;
    int pos = c1;
    for (int j = r1; j != r2; j+=dx) {
        pos = min(pos,len[j]+1);
    }
    pos = min(pos,len[r2]+1);
    ans = min(ans, abs(c2-pos) + abs(r1-r2));
    int pos2 = pos;
    for (int j = r2; j <= n && j >= 1; j+=dx) {
        pos2 = min(pos2,len[j]+1);
        ans = min(ans,2*abs(r2-j)+abs(r1-r2)+abs(c2-pos2));
    }
    int pos3 = pos;
    dx*=-1;
    for (int j = r1; j <= n && j >= 1; j+=dx) {
        pos3 = min(pos3,len[j]+1);
        ans = min(ans,2*abs(r1-j)+abs(r1-r2)+abs(c2-pos3));
    }
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>
#define REP(i,a,b) for(int i=a;i<b;i++)
using namespace std;

int len[200];

int main() {
    freopen("output.txt","w",stdout);
    freopen("input.txt","r",stdin);
    int n;
    cin >> n;
    bool debug = false;
    if (n == 100) debug = true;
    REP(i,1,n+1) cin >> len[i];
    int r1,r2,c1,c2;
    cin >> r1 >> c1 >> r2 >> c2;
    int ans = 1e8;
    int dx = 1;
    if (r2 < r1) dx=-1;
    int pos = c1;
    for (int j = r1; j <= r2; j+=dx) {
        pos = min(pos,len[j]+1);
    }
    ans = min(ans, abs(c2-pos) + abs(r1-r2));
    if (ans== 2485 && debug){
        cout << 1 << " " << r1 << " " << c1 << " " << r2 << " " << c2 << " " << len[r1] << " " << len[c2] << endl;
    debug = false;}

    int pos2 = pos;
    for (int j = r2; j <= n && j >= 1; j+=dx) {
        pos2 = min(pos2,len[j]+1);
        ans = min(ans,2*abs(r2-j)+abs(r1-r2)+abs(c2-pos2));
    if (ans== 2485 && debug){
        cout << 1 << " " << r1 << " " << c1 << " " << r2 << " " << c2 << " " << len[r1] << " " << len[c2] << endl;
    debug = false;}
    }
    int pos3 = pos;
    dx*=-1;
    for (int j = r1; j <= n && j >= 1; j+=dx) {
        pos3 = min(pos3,len[j]+1);
        ans = min(ans,2*abs(r1-j)+abs(r1-r2)+abs(c2-pos3));
    if (ans== 2485 && debug){
        cout << 1 << " " << r1 << " " << c1 << " " << r2 << " " << c2 << " " << len[r1] << " " << len[c2] << endl;
    debug = false;}
    }
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>
#define REP(i,a,b) for(int i=a;i<b;i++)
using namespace std;

int len[200];

int main() {
    ios_base::sync_with_stdio(0);
    freopen("output.txt","w",stdout);
    freopen("input.txt","r",stdin);
    int n;
    cin >> n;
    REP(i,1,n+1) cin >> len[i];
    int r1,r2,c1,c2;
    cin >> r1 >> c1 >> r2 >> c2;
    int ans = 1e8;
    int dx = 1;
    if (r2 < r1) dx=-1;
    int pos = c1;
    for (int j = r1; j <= r2; j+=dx) {
        pos = min(pos,len[j]+1);
    }
    ans = min(ans, abs(c2-pos) + abs(r1-r2));
    int pos2 = pos;
    for (int j = r2; j <= n && j >= 1; j+=dx) {
        pos2 = min(pos2,len[j]+1);
        ans = min(ans,2*abs(r2-j)+abs(r1-r2)+abs(c2-pos2));
    }
    int pos3 = pos;
    dx*=-1;
    for (int j = r1; j <= n && j >= 1; j+=dx) {
        pos3 = min(pos3,len[j]+1);
        ans = min(ans,2*abs(r1-j)+abs(r1-r2)+abs(c2-pos3));
    }
    cout << ans;
    return 0;
}


# include<iostream>
# include<cstdio>
# include<set>
# include<queue>
# define GI ({int t; scanf("%d", &t); t;})

using namespace std;

int A[105] ={0};
set <pair <int, int> > explored;

int main() {
freopen("input.txt","r",stdin);
freopen("output.txt", "w", stdout);
int n;
explored.clear();
int r1,c1,r2,c2;
n = GI;
for (int i = 0; i< n; i++) {
A[i] = GI;
}
pair <int, int> start;
pair <int, int> goal;
start.first = GI;
start.second = GI;
goal.first = GI;
goal.second = GI;
queue <pair <int, int> > pos;
queue <int> times;
times.push(0);
pos.push(start);
explored.insert(start);
while (!pos.empty()) {
    int timet = times.front();
    times.pop();
    pair <int, int> curr;
    pair <int, int> next;
    curr = pos.front();
    pos.pop();
    next.first = curr.first+1;
    next.second = curr.second;
    if (A[curr.first] + 1 < next.second) next.second = A[curr.first] + 1;
    if (curr.first < n && explored.find(next) == explored.end()) {
        if (next.first == goal.first && next.second == goal.second) {
        printf ("%d\n", timet+1);
        return 0;}
        pos.push(next);
        explored.insert(next);
        times.push(timet+1);
    }
    next.second = curr.second;
    if (A[curr.first - 2] + 1 < curr.second)
        next.second = A[curr.first - 2] + 1;
    next.first = curr.first-1;
    if (curr.first > 1 && explored.find(next) == explored.end()) {
        if (next.first == goal.first && next.second == goal.second) {
        printf ("%d\n", timet+1);
        return 0;
        }
        pos.push(next);
        explored.insert(next);
        times.push(timet+1);
    }
    next.first = curr.first;
        next.second = curr.second - 1;
    if (curr.second > 1 && explored.find(next) == explored.end()) {
        if (next.first == goal.first && next.second == goal.second) {
        printf ("%d\n", timet+1);
        return 0;}
        pos.push(next);
        explored.insert(next);
        times.push(timet+1);
    }
    next.first = curr.first;
    next.second = curr.second+1;
    if (curr.second < A[curr.first-1]+1 && explored.find(next) == explored.end()) {

        if (next.first == goal.first && next.second == goal.second) {
        printf ("%d\n", timet+1);
        return 0;}
        pos.push(next);
        explored.insert(next);
        times.push(timet+1);
    }
}
return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define sz size()
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define INF INT_MAX
#define MAX 100001
#define rep(i,n) for(int i = 0; i < n; i++)
typedef pair<int,int> pii;
typedef vector<int>       vi;
map<pii,int> M;
map<int,int> V;
queue<pii> q;

void go(int z,int w,int u,int v)
{
    if(M[mp(z,w)]<0)
            {
                q.push(mp(z,w));
                M[mp(z,w)]=M[mp(u,v)]+1;
            }
}

int main()
{
    freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    int n,r1,c1,r2,c2,a,u,v;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a;
        V[i]=a;
        for(int j=1;j<=a+1;j++)
            M[mp(i,j)]=-1;
    }
    cin>>r1>>c1>>r2>>c2;
    pii p(r1,c1);
    M[p]=0;
    q.push(p);
    while(!q.empty())
    {
        pii tmp=q.front();
        q.pop();
        u=tmp.x;
        v=tmp.y;
        if(u==r2 && v==c2)
        {
            cout<<M[tmp];
            return 0;
        }
        //up
        if(u!=1)
        {
            if(V[u-1]<v)
                go(u-1,V[u-1]+1,u,v);
            else
                go(u-1,v,u,v);
        }
        //down
        if(u!=n)
        {
            if(V[u+1]<v)
                go(u+1,V[u+1]+1,u,v);
            else
            go(u+1,v,u,v);
        }
        //left
        if(v>1)
            go(u,v-1,u,v);
        //right
        if(v<V[u]+1)
            go(u,v+1,u,v);
    }

}


#include <bits/stdc++.h>
using namespace std;
#define sz size()
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define INF INT_MAX
#define NMAX 105
#define MMAX 100005
#define rep(i,n) for(int i = 0; i < n; i++)
typedef pair<int,int> pii;
typedef vector<int>       vi;
int M[NMAX][MMAX];
int V[NMAX];
queue<pii> q;

void go(int z,int w,int u,int v)
{
    if(!M[z][w])
            {
                q.push(mp(z,w));
                M[z][w]=M[u][v]+1;
            }
}

int main()
{
    freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
        memset(M,0,sizeof(M[0][0]) * NMAX * MMAX);
    int n,r1,c1,r2,c2,a,u,v;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&V[i]);

    scanf("%d %d %d %d",&r1,&c1,&r2,&c2);
    pii p(r1,c1);
    q.push(p);
    while(!q.empty())
    {
        pii tmp=q.front();
        q.pop();
        u=tmp.x;
        v=tmp.y;
        if(u==r2 && v==c2)
        {
            cout<<M[u][v];
            return 0;
        }
        //up
        if(u!=1)
        {
            if(V[u-1]<v)
                go(u-1,V[u-1]+1,u,v);
            else
                go(u-1,v,u,v);
        }
        //down
        if(u!=n)
        {
            if(V[u+1]<v)
                go(u+1,V[u+1]+1,u,v);
            else
            go(u+1,v,u,v);
        }
        //left
        if(v>1)
            go(u,v-1,u,v);
        //right
        if(v<V[u]+1)
            go(u,v+1,u,v);
    }

}


#include <bits/stdc++.h>
using namespace std;
#define sz size()
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define INF INT_MAX
#define MAX 100001
#define rep(i,n) for(int i = 0; i < n; i++)
typedef pair<int,int> pii;
typedef vector<int>       vi;
map<pii,int> M;
map<int,int> V;
queue<pii> q;

void go(int z,int w,int u,int v)
{
    if(M.find(mp(z,w))==M.end())
            {
                q.push(mp(z,w));
                M[mp(z,w)]=M[mp(u,v)]+1;
            }
}

int main()
{
    freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    int n,r1,c1,r2,c2,a,u,v;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a);
        V[i]=a;
    }
    scanf("%d %d %d %d",&r1,&c1,&r2,&c2);
    pii p(r1,c1);
    M[p]=0;
    q.push(p);
    while(!q.empty())
    {
        pii tmp=q.front();
        q.pop();
        u=tmp.x;
        v=tmp.y;
        if(u==r2 && v==c2)
        {
            cout<<M[tmp];
            return 0;
        }
        //up
        if(u!=1)
        {
            if(V[u-1]<v)
                go(u-1,V[u-1]+1,u,v);
            else
                go(u-1,v,u,v);
        }
        //down
        if(u!=n)
        {
            if(V[u+1]<v)
                go(u+1,V[u+1]+1,u,v);
            else
            go(u+1,v,u,v);
        }
        //left
        if(v>1)
            go(u,v-1,u,v);
        //right
        if(v<V[u]+1)
            go(u,v+1,u,v);
    }

}


#include <bits/stdc++.h>
using namespace std;
#define sz size()
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define INF INT_MAX
#define NMAX 105
#define MMAX 100005
#define rep(i,n) for(int i = 0; i < n; i++)
typedef pair<int,int> pii;
typedef vector<int>       vi;
int M[NMAX][MMAX];
int V[NMAX];
queue<pii> q;

void go(int z,int w,int u,int v)
{
    if(!M[z][w])
            {
                q.push(mp(z,w));
                M[z][w]=M[u][v]+1;
            }
}

int main()
{
    freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
        //memset(M,0,sizeof(M[0][0]) * NMAX * MMAX);
    int n,r1,c1,r2,c2,a,u,v;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&V[i]);

    scanf("%d %d %d %d",&r1,&c1,&r2,&c2);
    pii p(r1,c1);
    q.push(p);
    while(!q.empty())
    {
        pii tmp=q.front();
        q.pop();
        u=tmp.x;
        v=tmp.y;
        if(u==r2 && v==c2)
        {
            cout<<M[u][v];
            return 0;
        }
        //up
        if(u!=1)
        {
            if(V[u-1]<v)
                go(u-1,V[u-1]+1,u,v);
            else
                go(u-1,v,u,v);
        }
        //down
        if(u!=n)
        {
            if(V[u+1]<v)
                go(u+1,V[u+1]+1,u,v);
            else
            go(u+1,v,u,v);
        }
        //left
        if(v>1)
            go(u,v-1,u,v);
        //right
        if(v<V[u]+1)
            go(u,v+1,u,v);
    }

}


#include <bits/stdc++.h>
using namespace std;
#define sz size()
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define INF INT_MAX
#define MAX 100001
#define rep(i,n) for(int i = 0; i < n; i++)
typedef pair<int,int> pii;
typedef vector<int>       vi;
map<pii,int> M;
map<int,int> V;
queue<pii> q;

void go(int z,int w,int u,int v)
{
    if(M.find(mp(z,w))==M.end())
            {
                q.push(mp(z,w));
                M[mp(z,w)]=M[mp(u,v)]+1;
            }
}

int main()
{
    freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    int n,r1,c1,r2,c2,a,u,v;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a;
        V[i]=a;
    }
    cin>>r1>>c1>>r2>>c2;
    pii p(r1,c1);
    M[p]=0;
    q.push(p);
    while(!q.empty())
    {
        pii tmp=q.front();
        q.pop();
        u=tmp.x;
        v=tmp.y;
        if(u==r2 && v==c2)
        {
            cout<<M[tmp];
            return 0;
        }
        //up
        if(u!=1)
        {
            if(V[u-1]<v)
                go(u-1,V[u-1]+1,u,v);
            else
                go(u-1,v,u,v);
        }
        //down
        if(u!=n)
        {
            if(V[u+1]<v)
                go(u+1,V[u+1]+1,u,v);
            else
            go(u+1,v,u,v);
        }
        //left
        if(v>1)
            go(u,v-1,u,v);
        //right
        if(v<V[u]+1)
            go(u,v+1,u,v);
    }

}


#include <bits/stdc++.h>
using namespace std;
#define sz size()
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define INF INT_MAX
#define MAX 100001
#define rep(i,n) for(int i = 0; i < n; i++)
typedef pair<int,int> pii;
typedef vector<int>       vi;
map<pii,int> M;
map<int,int> V;
queue<pii> q;

void go(int z,int w,int u,int v)
{
    if(M[mp(z,w)]<0)
            {
                q.push(mp(z,w));
                M[mp(z,w)]=M[mp(u,v)]+1;
            }
}

int main()
{
    int n,r1,c1,r2,c2,a,u,v;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a;
        V[i]=a;
        for(int j=1;j<=a+1;j++)
            M[mp(i,j)]=-1;
    }
    cin>>r1>>c1>>r2>>c2;
    pii p(r1,c1);
    M[p]=0;
    q.push(p);
    while(!q.empty())
    {
        pii tmp=q.front();
        q.pop();
        u=tmp.x;
        v=tmp.y;
        if(u==r2 && v==c2)
        {
            cout<<M[tmp];
            return 0;
        }
        //up
        if(u!=1)
        {
            if(V[u-1]<v)
                go(u-1,V[u-1]+1,u,v);
            else
                go(u-1,v,u,v);
        }
        //down
        if(u!=n)
        {
            if(V[u+1]<v)
                go(u+1,V[u+1]+1,u,v);
            else
            go(u+1,v,u,v);
        }
        //left
        if(v>1)
            go(u,v-1,u,v);
        //right
        if(v<V[u]+1)
            go(u,v+1,u,v);
    }

}


#include<bits/stdc++.h>
using namespace std;
#define ll  int
#define pi pair<ll,ll>
#define f first
#define s second
#define mp make_pair
int vis[101][1001];
int a[101];
ll n;
bool exists(ll x,ll y){
if((x>=1)&&(x<=n)&&(y<=a[x]+1)&&(y>=1)) return 1;
return 0;
}
int main(){
freopen("output.txt","w",stdout);
freopen("input.txt","r",stdin);
cin>>n;
for(int i=1;i<=n ;i++)
{
 cin>>a[i];
}
ll r1,c1,r2,c2;
cin>>r1>>c1>>r2>>c2;
queue<pi>q;
q.push(mp(r1,c1));
vis[r1][c1]=1;
pi it;
while(q.size()){
it=q.front();
q.pop();
if(it.f==r2&&it.s==c2) break;
if(exists(it.f,it.s-1)&&!vis[it.f][it.s-1]){
  vis[it.f][it.s-1]=vis[it.f][it.s]+1;
  q.push(mp(it.f,it.s-1));
}
if(exists(it.f,it.s+1)&&!vis[it.f][it.s+1]){
  vis[it.f][it.s+1]=vis[it.f][it.s]+1;
  q.push(mp(it.f,it.s+1));
}
if(it.f>1){
  if(exists(it.f-1,it.s)&&a[it.f-1]+1>=it.s&&!vis[it.f-1][it.s]){
    q.push(mp(it.f-1,it.s));
    vis[it.f-1][it.s]=1+vis[it.f][it.s];
  }
  else if(!vis[it.f-1][a[it.f-1]+1]&&exists(it.f-1,a[it.f-1]+1)){
    q.push(mp(it.f-1,a[it.f-1]+1));
    vis[it.f-1][a[it.f-1]+1]=1+vis[it.f][it.s];
  }
}
if(it.f<n){
if(a[it.f+1]+1>=it.s&&!vis[it.f+1][it.s]&&exists(it.f+1,it.s)){
    q.push(mp(it.f+1,it.s));
    vis[it.f+1][it.s]=1+vis[it.f][it.s];
  }
  else if(!vis[it.f+1][a[it.f+1]+1]&&exists(it.f+1,a[it.f+1]+1)){
    q.push(mp(it.f+1,a[it.f+1]+1));
    vis[it.f+1][a[it.f+1]+1]=1+vis[it.f][it.s];
  }
}
}
cout<<vis[r2][c2]-1;
}