Vasya is pressing the keys on the keyboard reluctantly, squeezing out his ideas on the classical epos depicted in Homer's Odysseus... How can he explain to his literature teacher that he isn't going to become a writer? In fact, he is going to become a programmer. So, he would take great pleasure in writing a program, but none — in writing a composition.

As Vasya was fishing for a sentence in the dark pond of his imagination, he suddenly wondered: what is the least number of times he should push a key to shift the cursor from one position to another one?

Let's describe his question more formally: to type a text, Vasya is using the text editor. He has already written n lines, the i-th line contains a_{i} characters (including spaces). If some line contains k characters, then this line overall contains (k + 1) positions where the cursor can stand: before some character or after all characters (at the end of the line). Thus, the cursor's position is determined by a pair of integers (r, c), where r is the number of the line and c is the cursor's position in the line (the positions are indexed starting from one from the beginning of the line).

Vasya doesn't use the mouse to move the cursor. He uses keys "Up", "Down", "Right" and "Left". When he pushes each of these keys, the cursor shifts in the needed direction. Let's assume that before the corresponding key is pressed, the cursor was located in the position (r, c), then Vasya pushed key: "Up": if the cursor was located in the first line (r = 1), then it does not move. Otherwise, it moves to the previous line (with number r - 1), to the same position. At that, if the previous line was short, that is, the cursor couldn't occupy position c there, the cursor moves to the last position of the line with number r - 1; "Down": if the cursor was located in the last line (r = n), then it does not move. Otherwise, it moves to the next line (with number r + 1), to the same position. At that, if the next line was short, that is, the cursor couldn't occupy position c there, the cursor moves to the last position of the line with number r + 1; "Right": if the cursor can move to the right in this line (c < a_{r} + 1), then it moves to the right (to position c + 1). Otherwise, it is located at the end of the line and doesn't move anywhere when Vasya presses the "Right" key; "Left": if the cursor can move to the left in this line (c > 1), then it moves to the left (to position c - 1). Otherwise, it is located at the beginning of the line and doesn't move anywhere when Vasya presses the "Left" key.

You've got the number of lines in the text file and the number of characters, written in each line of this file. Find the least number of times Vasya should push the keys, described above, to shift the cursor from position (r_1, c_1) to position (r_2, c_2).


-----Input-----

The first line of the input contains an integer n (1 ≤ n ≤ 100) — the number of lines in the file. The second line contains n integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ 10^5), separated by single spaces. The third line contains four integers r_1, c_1, r_2, c_2 (1 ≤ r_1, r_2 ≤ n, 1 ≤ c_1 ≤ a_{r}_1 + 1, 1 ≤ c_2 ≤ a_{r}_2 + 1).


-----Output-----

Print a single integer — the minimum number of times Vasya should push a key to move the cursor from position (r_1, c_1) to position (r_2, c_2).


-----Examples-----
Input
4
2 1 6 4
3 4 4 2

Output
3

Input
4
10 5 6 4
1 11 4 2

Output
6

Input
3
10 1 10
1 10 1 1

Output
3



-----Note-----

In the first sample the editor contains four lines. Let's represent the cursor's possible positions in the line as numbers. Letter s represents the cursor's initial position, letter t represents the last one. Then all possible positions of the cursor in the text editor are described by the following table.

123

12

123s567

1t345

One of the possible answers in the given sample is: "Left", "Down", "Left".

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Graph algorithms

Graph traversal

Shortest paths

Greedy algorithms

Data structures


Solutions:
import sys
from collections import *

def arr_inp(n):
	return [int(x) for x in sys.stdin.readline().split()]

def up():
	nonlocal ans
	(mi, c) = (pos[1], 0)
	for i in range(pos[0], n + 1):
		mi = min(mi, cols[i - 1] + 1)
		if i == pos[2]:
			ans = min(ans, pos[2] - pos[0] + abs(mi - pos[3]))
		elif i > pos[2]:
			if pos[2] < pos[0]:
				ans = min(ans, pos[0] - pos[2] + 2 * c + abs(min(min(cols[pos[2] - 1:pos[0]]) + 1, mi) - pos[3]))
			else:
				ans = min(ans, i - pos[2] + c + abs(min(min(cols[pos[0]:i]) + 1, mi) - pos[3]))
		c += 1

def down():
	nonlocal ans
	(mi, c) = (pos[1], 1)
	for i in range(pos[0] - 1, 0, -1):
		mi = min(mi, cols[i - 1] + 1)
		if i == pos[2]:
			ans = min(ans, pos[0] - pos[2] + abs(mi - pos[3]))
		elif i < pos[2]:
			if pos[2] >= pos[0]:
				ans = min(ans, pos[2] - pos[0] + 2 * c + abs(min(min(cols[pos[0] - 1:pos[2]]) + 1, mi) - pos[3]))
			else:
				ans = min(ans, pos[2] - i + c + abs(min(min(cols[i:pos[0]]) + 1, mi) - pos[3]))
		c += 1
sys.stdin = open('input.txt')
sys.stdout = open('output.txt', 'w')
(n, cols, pos) = (int(sys.stdin.readline()), arr_inp(1), arr_inp(1))
ans = float('inf')
up()
down()
print(ans)


import sys
sys.stdin = open('input.txt')
sys.stdout = open('output.txt', 'w')
n = int(input())
a = list(map(int, input().split()))
(r1, c1, r2, c2) = (i - 1 for i in map(int, input().split()))
(x, y) = (r1, r2) if r1 < r2 else (r2, r1)
if r1 < r2:
	c1 = min(c1, min((a[i] for i in range(r1 + 1, r2 + 1))))
elif r2 < r1:
	c1 = min(c1, min((a[i] for i in range(r2, r1))))
d = abs(c1 - c2)
c = c1
for i in range(x - 1, -1, -1):
	if c > a[i]:
		c = a[i]
		q = abs(c2 - c) + 2 * abs(x - i)
		if q < d:
			d = q
c = c1
for i in range(y + 1, n):
	if c > a[i]:
		c = a[i]
		q = abs(c2 - c) + 2 * abs(i - y)
		if q < d:
			d = q
print(d + abs(r2 - r1))


import sys
sys.stdin = open('input.txt')
sys.stdout = open('output.txt', 'w')
n = int(input())
a = [int(x) for x in input().split()]
[r1, c1, r2, c2] = [int(x) for x in input().split()]
r1 -= 1
r2 -= 1
c1 -= 1
c2 -= 1
dr = r2 - r1
ddr = dr // abs(dr) if dr != 0 else 1
c = c1
for i in range(abs(dr) + 1):
	r = r1 + ddr * i
	c = min(c, a[r])
pen1 = 0
for i in range(min(r1, r2)):
	pen = (min(r1, r2) - i) * 2
	if c > c2 and a[i] < c and (a[i] <= min(a[i:min(r1, r2)])):
		pen -= c - c2 - abs(a[i] - c2)
	pen1 = min(pen1, pen)
pen2 = 0
for i in range(max(r1, r2) + 1, n):
	pen = (i - max(r1, r2)) * 2
	if c > c2 and a[i] < c and (a[i] <= min(a[max(r1, r2):i])):
		pen -= c - c2 - abs(a[i] - c2)
	pen2 = min(pen2, pen)
pen = abs(dr) + abs(c - c2)
print(min(pen + pen1, pen + pen2))


from collections import deque

def read(line):
	return [int(c) for c in line.split()]

def solve(n, lines, r1, c1, r2, c2):
	queue = deque([(r1, c1, 0)])
	visited = {}
	while queue:
		(r, c, cost) = queue.pop()
		if (r, c) not in visited:
			visited[r, c] = cost
		elif cost < visited[r, c]:
			visited[r, c] = cost
		else:
			continue
		if c2 <= lines[r - 1] + 1:
			queue.appendleft((r, c2, cost + abs(c - c2)))
		last_pos = lines[r - 1] + 1
		if c < last_pos:
			queue.appendleft((r, last_pos, cost + abs(c - last_pos)))
		if r - 1 >= 1:
			last_pos_prev = lines[r - 2] + 1
			if last_pos_prev >= c:
				queue.appendleft((r - 1, c, cost + 1))
			else:
				queue.appendleft((r - 1, last_pos_prev, cost + 1))
		if r + 1 <= n:
			last_pos_next = lines[r] + 1
			if last_pos_next >= c:
				queue.appendleft((r + 1, c, cost + 1))
			else:
				queue.appendleft((r + 1, last_pos_next, cost + 1))
	return visited[r2, c2]

def main():
	with open('input.txt') as f:
		test = f.readlines()
	(n,) = read(test[0])
	lines = read(test[1])
	(r1, c1, r2, c2) = read(test[2])
	ans = solve(n, lines, r1, c1, r2, c2)
	with open('output.txt', 'w') as f:
		f.write(str(ans))

def __starting_point():
	main()
__starting_point()


import sys
sys.stdin = open('input.txt')
sys.stdout = open('output.txt', 'w')
n = int(input())
a = list(map(int, input().split()))
(r1, c1, r2, c2) = list(map(int, input().split()))
r1 -= 1
r2 -= 1
c1 -= 1
c2 -= 1
sign = 1 if r2 - r1 >= 0 else -1
for i in range(abs(r2 - r1) + 1):
	c1 = min(c1, a[r1 + sign * i])
res1 = 0
for i in range(min(r1, r2)):
	tmp = (min(r1, r2) - i) * 2
	if c1 > max(c2, a[i]) and a[i] <= min(a[i:min(r1, r2)]):
		tmp -= c1 - c2 - abs(a[i] - c2)
	res1 = min(res1, tmp)
res2 = 0
for i in range(max(r1, r2) + 1, n):
	tmp = (i - max(r1, r2)) * 2
	if c1 > max(c2, a[i]) and a[i] <= min(a[max(r1, r2):i]):
		tmp -= c1 - c2 - abs(a[i] - c2)
	res2 = min(res2, tmp)
print(min(res1, res2) + abs(r2 - r1) + abs(c1 - c2))